2016-07-01 78 views
2

我有設立的Outlook郵件到JavaMail的頭痛,我曾跟隨從here和說明here的Outlook電子郵件設置用來爲JavaMail API

Properties props = new Properties(); 
      props.put("mail.smtp.user", d_email); 
      props.put("mail.smtp.host", "smtp-mail.outlook.com"); 
      props.put("mail.smtp.port", "587"); 
      props.put("mail.smtp.starttls.enable","true"); 
      props.put("mail.smtp.auth", "true"); 
      props.put("mail.smtp.socketFactory.port", "587"); 
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
      props.put("mail.smtp.socketFactory.fallback", "true"); 

      try 
      { 
      Authenticator auth = new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(d_email, d_password); 
       } 
      }; 

      Session session = Session.getInstance(props, auth); 

      MimeMessage msg = new MimeMessage(session); 
      msg.setText("Hey, this is the testing email."); 
      msg.setSubject("Testing"); 
      msg.setFrom(new InternetAddress(d_email)); 
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); 
      Transport.send(msg); 

      }catch (MessagingException mex) { 
      mex.printStackTrace(); 
      } 

而且我不斷收到錯誤:

javax.mail.MessagingException: Could not connect to SMTP host: smtp-mail.outlook.com, port: 587; 
    nested exception is: 
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) 
    at javax.mail.Service.connect(Service.java:317) 
    at javax.mail.Service.connect(Service.java:176) 
    at javax.mail.Service.connect(Service.java:125) 

我已經搜索谷歌和stackoverflow,我想這是一個必須使用SSL而不是TLS,因爲outlook不接受TLS,否則連接將被遠程主機關閉,但我從互聯網上找到的解決方案並沒有工作出。我爲了成功發送測試郵件而錯過了什麼?

我是全新的郵件API,任何建議或幫助將不勝感激。謝謝。

編輯:我從微軟我註冊類似「[email protected]

+0

噢,我的上帝的工作,我終於解決它,當我登錄到我的outlook.live.com電子郵件,我看到一個消息來自outlook團隊的郵件,表示要繼續發送郵件,請登錄並驗證我的帳戶。幫助我們阻止發送垃圾郵件的自動程序。在我驗證了我的電子郵件之後,哦,我的上帝 –

+0

有趣。從未見過。 –

+0

無論如何感謝你們的努力和一切,一定要檢查回發送郵箱下次哈哈 –

回答

1

刪除的SocketFactory參數,像這樣的電子郵件帳戶,並享受發送電子郵件。

props.put("mail.smtp.host", "smtp-mail.outlook.com"); 
props.put("mail.smtp.port", "587"); 
props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.auth", "true"); 

我在文檔中找不到這個參數。

https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

+0

該死的。 12秒;-) –

+0

請參考我的問題評論,謝謝你的努力!讚賞 –

1

請重試沒有這些道具:

props.put("mail.smtp.socketFactory.port", "587"); 
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.put("mail.smtp.socketFactory.fallback", "true"); 

我沒有設置他們office365。 outlook.com應該similiar

+0

請參閱我的問題評論,謝謝你的努力!讚賞 –

0

試試這個代碼,它是爲我

Properties props = null; 
    if (props == null) { 
     props = new Properties(); 
     props.put("mail.smtp.auth", true); 
     props.put("mail.smtp.starttls.enable", true); 
     props.put("mail.smtp.host", "smtp.live.com"); 
     props.put("mail.smtp.port", "587"); 
     props.put("mail.smtp.user", User); 
     props.put("mail.smtp.pwd", Pwd); 
    } 
    Session session = Session.getInstance(props, null); 
    session.setDebug(true); 
    Message msg = new MimeMessage(session); 
    msg.setFrom(new InternetAddress(User)); 
    if (Objet != null) { 
     msg.setSubject(Objet); 

    } 
    msg.setText(Content); 
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailId)); 
    Transport transport = session.getTransport("smtp"); 
    transport.connect("smtp.live.com", 587, User, Pwd); 
    transport.sendMessage(msg, msg.getAllRecipients()); 
    System.out.println("Mail sent successfully at " + emailId); 
    transport.close(); 
}