从Gmail smtp服务器通过SSL使用javax邮件API发送邮件时出现异常

时间:2017-11-15 10:23:32

标签: smtp javamail

对于以下Java代码,我无法将邮件发送到所需目的地。

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Authenticator;

@SpringBootApplication

public class SmtpdemoApplication {

public static void main(String[] args) {
    String uname = "from@gmail.com";
    String passwd = "to@gmail.com";
    String from = "from@gmail.com";
    String to = "to@gmail.com";
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
    "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(uname, passwd);
                }
            });
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject("Testing Subject");
        message.setText("Testing Text");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
  }
}

我得到以下例外

Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Invalid Addresses;
 nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 Relay access denied

at Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?   at  com.smtpdemo.SmtpdemoApplication.main(SmtpdemoApplication.java:56)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2102)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at com.smtpdemo.SmtpdemoApplication.main(SmtpdemoApplication.java:51)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext  connection?
at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:710)
at sun.security.ssl.InputRecord.read(InputRecord.java:527)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:598)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:372)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)
... 7 more

我试图放一个props.put("mail.smtp.ssl.enable", "true"),但这似乎也没有用。我怀疑我正在配置的属性有一些错误,但我还没有找到解决方案。我还提到了Java邮件的this FAQ部分。

0 个答案:

没有答案