【问题标题】:How to send email in java GWT如何在 Java GWT 中发送电子邮件
【发布时间】:2016-05-30 03:19:35
【问题描述】:

我在我的 java GWT 应用程序中使用此代码

        public String greetServer(String input) throws Exception {
    try{
    Properties props = new Properties();

     props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.smtp.port", "25");
    props.setProperty("mail.host", "smtp.random.com");
    props.setProperty("mail.user", "foo@bar.com");
    props.setProperty("mail.password", "000000000");

    Session mailSession = Session.getDefaultInstance(props, null);
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject("hello");
    message.setContent("helloo sss", "text/plain");
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("junaidp@gmail.com"));

    transport.connect();
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();
    } catch(NoSuchProviderException e){
        throw new Exception(e);
      }

    return input;

}

错误:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.random.com,端口:25; 嵌套异常是: java.net.ConnectException:连接被拒绝:连接

如果我使用

            props.setProperty("mail.host", "smtp.live.com");
            and use my hotmail account , it gives this error 

           javax.mail.MessagingException: can't determine local email address

知道解决办法是什么

谢谢

【问题讨论】:

  • 连接到正在监听的 SMTP 服务器?

标签: java gwt


【解决方案1】:

我刚刚在 GWT 项目中使用了Simple Java Mail。您可能想尝试一下。配置非常简单。

那里有很多例子,包括一个使用gmail's SMTP server using for example TLS发送的例子:

Email email = new Email.Builder()
    .from("Michel Baker", "m.baker@mbakery.com")
    .to("mom", "jean.baker@hotmail.com")
    .to("dad", "StevenOakly1963@hotmail.com")
    .subject("My Bakery is finally open!")
    .text("Mom, Dad. We did the opening ceremony of our bakery!!!")
    .build();

new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email);

如果您启用了双因素登录,则需要从您的 Google 帐户生成应用程序专用密码。

【讨论】:

    【解决方案2】:

    以下是一些适合我的 Gmail 设置:

    //these are fed into the Properties object below:
    mail.smtp.starttls.enable = true
    mail.transport.protocol   = smtp
    mail.smtp.auth            = true
    

    还有一些 Java:

    Properties properties = ...
    
    javax.mail.Session session = javax.mail.Session.getInstance(properties, null);
    Transport transport = session.getTransport("smtp");
    transport.connect("smtp.gmail.com", username, password);
    

    【讨论】:

      【解决方案3】:
      Error: javax.mail.MessagingException: Could not connect 
      to SMTP host: smtp.random.com port: 25; 
      nested exception is: java.net.ConnectException: Connection refused: connect
      

      此错误表示您提供的 SMTP 服务器无效。您的代码是正确的,但 smtp.random.com 不能是有效的 SMTP 服务器。

      如果您使用有效的 gmail 帐户,我建议您考虑使用免费的 Google SMTP 服务器。

      有关使用 Gmail 的 STMP 服务器的更多信息,请参阅此页面:http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm

      【讨论】:

      • 谢谢...如果我将 smtp.gmail.com 或 smtp.live.com 与 hotmail 帐户一起使用,我会收到:javax.mail.MessagingException:无法确定本地电子邮件地址... . 对此有任何想法
      【解决方案4】:
      猜你喜欢
      • 2010-10-27
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 2012-04-17
      • 2023-04-10
      • 2018-05-10
      • 1970-01-01
      相关资源
      最近更新 更多