如何在java GWT中发送电子邮件

时间:2013-09-03 18:00:44

标签: java gwt

我在我的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,port: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

知道可能是什么解决方案

感谢

4 个答案:

答案 0 :(得分:2)

我刚刚在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帐户生成应用专用密码。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

答案 3 :(得分:0)

以下是一些适用于我的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);