2015-07-21 124 views
4

嘗試通過Java Mail API發送電子郵件時出現以下錯誤?這個錯誤是什麼意思?通過Java Mail API發送郵件時出錯?

javax.mail.MessagingException: Exception reading response; 
    nested exception is: 
     java.net.SocketTimeoutException: Read timed out 
javax.mail.MessagingException: Exception reading response; 
    nested exception is: 
     java.net.SocketTimeoutException: Read timed out 
     at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2210) 
     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950) 
     at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642) 
     at javax.mail.Service.connect(Service.java:317) 
     at javax.mail.Service.connect(Service.java:176) 
     at javax.mail.Service.connect(Service.java:125) 
     at javax.mail.Transport.send0(Transport.java:194) 
     at javax.mail.Transport.send(Transport.java:124) 

這裏是我的代碼,我設置的所有參數(從,到,主題和附件)

public static void send(MailUtil mailUtil) throws MessagingException { 
      MimeMessage message = new MimeMessage(session); 
      if (props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_SENDER) != null) {    
       message.setFrom(new InternetAddress(props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_SENDER))); 
      } else { 
       message.setFrom(new InternetAddress(mailUtil.getFrom())); 
      } 
      message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse(mailUtil.getTo())); 
      if (mailUtil.getBcc() != null && mailUtil.getBcc().trim().length() > 0) { 
       message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mailUtil.getBcc())); 
      } else { 
       message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("")); 
      } 

      message.setSubject(mailUtil.getSubject(), "UTF-8"); 

      // Check for files list and attach them. 
      if (mailUtil.attachmentFiles != null && mailUtil.attachmentFiles.size() > 0) { 
       Multipart multipart = new MimeMultipart(); 

       // Set content. 
       BodyPart messageBodyPart =new MimeBodyPart(); 
       messageBodyPart.setContent(mailUtil.getContent(), "text/plain; charset=utf-8"); 
       multipart.addBodyPart(messageBodyPart); 

       // Attach files. 
       for (File file : mailUtil.attachmentFiles) { 
        messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(file); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(file.getName()); 
        multipart.addBodyPart(messageBodyPart); 
       } 

       message.setContent(multipart); 
      } else { 
       //message.setContent("<h1>Hello world</h1>", "text/html"); 
       message.setContent(mailUtil.getContent(), "text/html; charset=UTF-8"); 
      }   
      Transport.send(message); 
    } 

我只是覺得在那裏我paramters任何問題嗎?

初級講座是我的配置

mail.smtp.port=465 
mail.smtp.starttls.enable=true 
mail.smtp.auth=true 
mail.smtp.socketFactory.port=465 
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory 
mail.smtp.timeout=25000 

mail.smtp.host=smtp.gmail.com 
mail.username = [email protected] 
mail.password = mypassword 
mail.sender = [email protected] 
mail.receiver = [email protected] 
mail.subject = mysubject 

我使用谷歌的郵件服務器!我不認爲那裏有問題!

初級講座是會話發起

final String userName = props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_USERNAME); 
final String passWord = props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_PASSWORD); 

session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(userName, 
           passWord); 
        } 
      }); 
+2

請分享代碼,不要難以幫助您 –

+1

您需要有關使用url檢查MessageingException的信息http://stackoverflow.com/questions/5659325/getting-javax-mail-messagingexception-and-java-net- socketexception –

+0

有一段時間,所以你的電子郵件服務器可能無法訪問或屬性設置不正確。 – Typo

回答

5

我相信這絕對與服務器配置有關。 當我將端口配置從465更改爲587時,它解決了我的問題! 無論如何,謝謝你的幫助!

+0

正確,端口465希望您啓動SSL會話,然後在其上運行smtp,與端口587相比,會話以純文本SMTP開頭,然後starttls用於協商加密的TLS會話。所以,你的配置的前兩行是衝突的。 – mc0e

0

我不知道爲什麼你使用「郵件」對象設置連接屬性,而不是嘗試這個,它應該工作,我測試過自己:

private Properties props; 
props = System.getProperties(); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.port", "587"); 

session = Session.getInstance(props, 
    new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(username, password); 
     } 
     }); 
try { 
     message.setFrom(new InternetAddress("YOUR EMAIL ADDRESS HERE")); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress("RECEIVER EMAIL ADDRESS HERE")); 
     message.setSubject("SUBJECT"); 

     message.setText("THE EMAIL TEXT"); 
     Transport.send(message); 
    } catch (MessagingException e) {e.printStackTrace();} 
}