让JavaMail API工作......再次

时间:2015-03-15 22:59:16

标签: java email javamail java-api

我正在使用JavaMail API发送电子邮件的应用程序,但我一直在收到错误。我使用Eclipse对其进行编码,并且我使用gmail发送它。我出于显而易见的原因取出了我的真实密码,因此如果您需要进行实验,您可能需要用自己的密码替换它。既然我已经修复了一些东西,感谢你们,我想我得到了一个超时错误,因为它需要很长时间才能显示错误,但除此之外,我还没有最模糊的线索。提前感谢您的任何帮助或建议。

代码:

package com.brighamcampbell.sunrisegundersonmail;

import java.io.UnsupportedEncodingException;
import java.util.Properties;  

import javax.mail.*;  
import javax.mail.internet.*;  

public class Mail {  

    public static void main(String[] args) throws MessagingException,
    UnsupportedEncodingException {

Properties mailProps = new Properties();

mailProps.put("mail.smtp.from", "butterscotchdreamer23@gmail.com");
mailProps.put("mail.smtp.host", "smtp.gmail.com");
mailProps.put("mail.smtp.ssl.trust", "smtp.gmail.com");
mailProps.put("mail.smtp.port", "465");
mailProps.put("mail.smtp.auth", true);
mailProps.put("mail.smtp.starttls.enable", "true");

Session mailSession = Session.getDefaultInstance(mailProps, new Authenticator() {


    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("butterscotchdreamer23@gmail.com", "password");
    }

});


MimeMessage message = new MimeMessage(mailSession);

//set the email sender
message.setFrom(new InternetAddress("butterscotchdreamer23@gmail.com"));

//set the email recipients
String[] emails = { "butterscotchdreamer23@gmail.com" };

InternetAddress dests[] = new InternetAddress[emails.length];
for (int i = 0; i < emails.length; i++) {
    dests[i] = new InternetAddress(emails[i].trim().toLowerCase());
}
message.setRecipients(Message.RecipientType.TO, dests);

//set the email subject
message.setSubject("test");

//set the email content
message.setText("this is a test");

//send
System.out.println("sending...");
Transport.send(message);
System.out.println("done sending email!");

}
}  

错误:

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2041)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
at javax.mail.Service.connect(Service.java:386)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at com.brighamcampbell.sunrisegundersonmail.Mail.main(Mail.java:56)

感谢您的耐心等待!

3 个答案:

答案 0 :(得分:0)

显然,邮件服务器不支持STARTTLS,因此您不应该启用它。

答案 1 :(得分:0)

尝试删除3个sslfactory属性,因为javamail不再需要它。你应该能够以这种方式连接。

注意:gmail服务器肯定支持STARTTLS:

x@test:~/$ telnet smtp.gmail.com 587
Trying 74.125.136.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP vv9sm12826892wjc.35 - gsmtp
EHLO me
250-mx.google.com at your service, [147.58.51.121]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
QUIT

答案 2 :(得分:0)

常规Gmail说明为here,常规连接调试提示为here,常见错误列表为here。可能您遇到了阻止您尝试连接的防火墙或防病毒程序。如果仍然无法使其工作,请发布JavaMail调试输出。