2014-09-23 76 views
0

我能夠使用smtp.gmail.com主機發送郵件但不能使用smtp.live.com我也嘗試與主機有465,25和587端口。我無法使用javamail代碼發送郵件,但使用的是javamail代碼,但在Gmail中工作正常

我的配置文件是

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 

回答

0

我也遇到了這個問題,當您嘗試連接到安全的加密通道,而不SSL它發生以下。爲了解決這個問題,你必須添加屬性

mail.smtp.ssl.enable "true" 

如果此配置後獲得證書的問題,那麼你還需要導入服務器證書的信任存儲區。

相關問題