无法使用java邮件客户端发送邮件

时间:2015-12-30 11:45:44

标签: java email javamail

我已经查看了SO之前关于此事的链接,但他们没有解决我的问题。

所以这是我的代码:

public class SendMail {

    private String from;
    private String to;
    private String host;
    private String subject;
    private String message;
    private String pass;

    private static final Logger LOGGER = Logger.getLogger(SendMail.class.getName());

    public SendMail(String from, String pass, String to, String host, String subject, String message) {
        super();
        this.from = from;
        this.to = to;
        this.host = host;
        this.subject = subject;
        this.message = message;
        this.pass = pass;
    }

    public void sendMail(){
        Properties properties = System.getProperties();
        final String USERNAME = from;
        final String PASSWORD = pass;

        properties.put("mail.smtp.user", USERNAME);
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "25");
        properties.put("mail.debug", "true");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.EnableSSL.enable", "true");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallbac k", "false");
        properties.setProperty("mail.smtp.port", "465");
        properties.setProperty("mail.smtp.socketFactory.port", "465");

        Session session = Session.getInstance(properties, new javax.mail.Authenticator(){
            protected PasswordAuthentication getPasswordAuthenticated(){
                return new PasswordAuthentication(USERNAME, PASSWORD);
            }
        });


        //properties.setProperty("mail.imap.ssl.enable", "true");
        //properties.put("mail.debug", "true");
        //Session session = Session.getInstance(properties);

        try {
            //Store store = session.getStore("imap");
            //store.connect(host, USERNAME, PASSWORD);

            MimeMessage mime = new MimeMessage(session);
            mime.setFrom(new InternetAddress(USERNAME));
            mime.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            mime.setSubject(subject);
            mime.setText(message);
            Transport.send(mime);
            LOGGER.log(Level.INFO, "Mail sent successfully");
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            LOGGER.log(Level.SEVERE, "Unable to send mail\n");
        }   
    }

}

以下是日志:

DEBUG: JavaMail version 1.4.4
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
Dec 30, 2015 4:51:31 PM utility.mailclient.SendMail sendMail
SEVERE: Unable to send mail

此外,上述代码中的注释部分取自此链接:

http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail

应该是正确的。但我得到的结果相同。以下是使用此代码的日志(并注释掉上面的所有properties.putproperties.setProperty行)

DEBUG: JavaMail version 1.4.4
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 182.75.40.98 j10mb367075283iee
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH
A0 OK Thats all she wrote! j10mb367075283iee
DEBUG IMAP: AUTH: XOAUTH2
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: PLAIN-CLIENTTOKEN
DEBUG IMAP: AUTH: OAUTHBEARER
DEBUG IMAP: AUTH: XOAUTH
DEBUG: protocolConnect login, host=imap.gmail.com, user=mailid04@gmail.com, password=<non-null>
A1 AUTHENTICATE PLAIN
+ 
AGF0dWxzcGFtMDRAZ21haWwuY29tAGF0dWxzcGFt
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS
A1 OK mailid@gmail.com authenticated (Success)
A2 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS
A2 OK Success
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
Dec 30, 2015 5:11:24 PM utility.mailclient.SendMail sendMail
SEVERE: Unable to send mail

PS:我已经开启了gmail检查,允许安全性较低的应用访问它。

2 个答案:

答案 0 :(得分:2)

这可能不是一个完整的答案,但是有很多错误我不能在评论中完全适合......

  1. Get rid of the socket factory properties, you don't need them.
  2. 没有&#34; mail.smtp.EnableSSL.enable&#34;属性。也许你打算"mail.smtp.ssl.enable"
  3. 您使用的是旧版JavaMail upgrade if you can
  4. 更改您的Gmail密码立即,它在上面的日志输出中公开(但已编码)。
  5. 在使用“传输”发送邮件之前,无需连接到商店。
  6. Gmail示例位于JavaMail FAQ
  7. 您正在设置&#34; mail.smtp.port&#34;两次,到不同的价值观。如果设置&#34; mail.smtp.ssl.enable&#34;你根本不需要设置它。 to&#34; true&#34;。
  8. 如果您进行了所有这些更改但仍然无效,请使用最新代码和调试输出更新您的帖子。

答案 1 :(得分:0)

经过多次设置不同属性的试验后,我得到了能够发送邮件的代码。这是代码:

public void sendMail(){
    Properties properties = System.getProperties();
    final String USERNAME = from;
    final String PASSWORD = pass;

    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.user", from);
    properties.put("mail.smtp.password", pass);
    properties.put("mail.smtp.port", "587");
    properties.put("mail.smtp.auth", "true");
    //properties.put("mail.debug", "true");

    Session session = Session.getDefaultInstance(properties);
    try {

        MimeMessage mime = new MimeMessage(session);
        mime.setFrom(new InternetAddress(USERNAME));

        mime.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mime.setSubject(subject);
        mime.setText(message);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(mime, mime.getAllRecipients());
        transport.close();
        LOGGER.log(Level.INFO, "Mail sent successfully");
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        LOGGER.log(Level.ERROR, "Unable to send mail\n");
    }   
}

这里值得注意的变化是:

  • 设置的不同属性。
  • 上一次transport.send的方式更改为transport.sendMessage

这很有效。