我无法使用javamail代码发送带有outlook(hotmail帐户)配置的邮件,但在gmail工作正常

时间:2014-09-23 07:47:09

标签: java email outlook javamail hotmail

我可以使用smtp.gmail.com主机发送邮件但不能使用smtp.live.com我也尝试使用主机的465,25和587端口。

我的配置文件是

mail.smtp.host=smtp.live.com
mail.smtp.socketFactory.port=587
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.auth=true
mail.smtp.port=587

我使用以下代码发送邮件。

Properties props = new Properties();
            try {                      props.load(UtilEmail.class.getResourceAsStream("properties/email.properties"));              
            } catch (IOException e1) {
                StringWriter stack = new StringWriter();
                e1.printStackTrace(new PrintWriter(stack));
                log.error(stack.toString());
            }

            final String user_name=<<my email address>>;
            final String password="<<my password>>";
            Session session = Session.getInstance(props,
                      new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(user_name,password);
                        }
                      });


            Message message = new MimeMessage(session);
             message.setFrom(new InternetAddress(user_name,title));
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
             message.setSubject(subject);
             message.setContent(body,"text/html");
             Transport.send(message);

我得到了异常

    ERROR: util.UtilEmail - javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 587;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at org.snapsboard.core.util.UtilEmail.send(UtilEmail.java:49)
    at org.snapsboard.core.email.EmailService.inviteUserToJoinEvent(EmailService.java:315)
    at org.snapsboard.api.event.sendInvitaionMailUsringThread.run(sendInvitaionMailUsringThread.java:27)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:694)
    at sun.security.ssl.InputRecord.read(InputRecord.java:527)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
    ... 9 more

1 个答案:

答案 0 :(得分:0)

当您尝试连接到没有SSL的安全加密通道时,我也遇到过此问题。要解决此问题,您必须添加属性

mail.smtp.ssl.enable "true"

如果在此配置后出现证书问题,则还必须在信任库中导入服务器证书。