2017-04-04 163 views
1

我試圖通過outlook發送郵件,但收到錯誤如下。java outlook發送郵件

package test.first.javamail; 

import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

public class SendAttachmentInEmail { 
    public static void main(String[] args) { 
     // Recipient's email ID needs to be mentioned. 
     // String to = "[email protected]"; 

     // Sender's email ID needs to be mentioned 
     String from = "[email protected]"; 
     final String password = "*****";// change accordingly 

     // Check how many arguments were passed in 
     if (args.length == 0) { 
      System.out.println("Please run Sendmail jar as: java -jar SendMail.jar <[email protected]> <path of attachment file>"); 
      System.exit(0); 
     } 

     String to = args[0]; 
     String attachment_path = args[1]; 
     final String username = from; 
     ;// change accordingly 

     System.out.println("Trying to send mail to : " + to + "\n"); 
     // final String username = "abcd";// change accordingly 
     // final String password = "******";// change accordingly 
     System.out.println("Please wait for a moment, we are checking creadentials...! \n"); 
     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
//  props.put("mail.smtp.host", "smtp.gmail.com");smtp.office365.com 
     props.put("mail.smtp.host", "smtp.office365.com"); 
     props.put("mail.smtp.port", "587"); 
//  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
//  props.put("mail.smtp.socketFactory.fallback", "true"); 

     // Get the Session object. 
     Session session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(username, password); 
        } 
       }); 
     System.out.println("Seat tight your identity has been proved and mail is just on the way...! "); 
     try { 
      // Create a default MimeMessage object. 
      Message message = new MimeMessage(session); 

      // Set From: header field of the header. 
      message.setFrom(new InternetAddress(from)); 

      // Set To: header field of the header. 
      message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse(to)); 

      // Set Subject: header field 
      message.setSubject("Testing Subject"); 

      // Create the message part 
      BodyPart messageBodyPart = new MimeBodyPart(); 

      // Now set the actual message 
      messageBodyPart.setText("This is message body"); 

      // Create a multipar message 
      Multipart multipart = new MimeMultipart(); 

      // Set text message part 
      multipart.addBodyPart(messageBodyPart); 

      // Part two is attachment 
      messageBodyPart = new MimeBodyPart(); 
      String filename = attachment_path;// "C:/Users/swapnil.kotwal/Desktop/buildsuccess.txt";Azure.png 
      DataSource source = new FileDataSource(filename); 
      messageBodyPart.setDataHandler(new DataHandler(source)); 
      messageBodyPart.setFileName(filename); 
      multipart.addBodyPart(messageBodyPart); 

      // Send the complete message parts 
      message.setContent(multipart); 

      // Send message 
      Transport.send(message); 

      System.out.println("Sent message successfully...."); 

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

錯誤是

Exception in thread "main" java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1; 
     nested exception is: 
      java.net.ConnectException: Connection timed out: connect 
      at test.first.javamail.SendAttachmentInEmail.main(SendAttachmentInEmail.java:104) 
    Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1; 
     nested exception is: 
      java.net.ConnectException: Connection timed out: connect 
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) 
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699) 
      at javax.mail.Service.connect(Service.java:367) 
      at javax.mail.Service.connect(Service.java:226) 
      at javax.mail.Service.connect(Service.java:175) 
      at javax.mail.Transport.send0(Transport.java:253) 
      at javax.mail.Transport.send(Transport.java:124) 
      at test.first.javamail.SendAttachmentInEmail.main(SendAttachmentInEmail.java:99) 
    Caused by: java.net.ConnectException: Connection timed out: connect 
      at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) 
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
      at java.net.PlainSocketImpl.connect(Unknown Source) 
      at java.net.SocksSocketImpl.connect(Unknown Source) 
      at java.net.Socket.connect(Unknown Source) 
      at java.net.Socket.connect(Unknown Source) 
      at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331) 
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238) 
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066) 

我相信代碼是正確的,但不能確定究竟是什麼阻止了郵件傳遞?任何幫助將不勝感激。

+0

檢查SMTP服務器和端口是否正確。 –

回答

0

基於

java.net.ConnectException: Connection timed out: connect 
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) 
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699) 

我認爲你需要檢查一些東西

  • SMTP服務器應適當
  • 一些組織已封鎖587端口號,請與您的IT檢查團隊如果該端口是開放的與給定的SMTP通信 服務器

如果您得到相應的開放端口,則問題會解決。

這一特性

 Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
//  props.put("mail.smtp.host", "smtp.gmail.com");smtp.office365.com 
     props.put("mail.smtp.host", "smtp.office365.com"); 
     props.put("mail.smtp.port", "587"); //change this port 

的基本工作原理。如果有任何代理相關的設置你的組織有,你可以參考這個答案以及https://stackoverflow.com/a/12097764/1665592

+1

可能是拒絕連接的代理。所以你也需要設置代理。 –

+0

謝謝,給出了代理設置的鏈接 –