2014-08-29 162 views
0

我正在使用java郵件發送郵件,我陷入了這個錯誤:javax.mail.MessagingException:無法連接到SMTP主機:103.12.134.112,端口:25; 我已經看到相關的問題,bt它不適合我,使用窗口8.幫助我,thankx提前。javax.mail.MessagingException:無法連接到SMTP主機:103.12.134.112,端口:25;

package com.airportbookingvn; 
    import java.io.ByteArrayOutputStream; 
    import java.io.OutputStream; 
    import java.util.Properties; 





    import javax.activation.DataHandler; 
    import javax.activation.DataSource; 
    import javax.mail.Message; 
    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; 
    import javax.mail.util.ByteArrayDataSource; 

    import com.lowagie.text.Chunk; 
    import com.lowagie.text.Document; 
    import com.lowagie.text.Paragraph; 
    import com.lowagie.text.pdf.PdfWriter; 



    /** 
    * Email with PDF example. 
    * <br><br> 
    * Email sending code adapted from http://www.java-tips.org/other-api-tips/javamail/how-to-send-an-email-with-a-file-attachment.html. 
    * @author Jee Vang 
    * 
    */ 
    public class SendAttachment { 

     /** 
     * Sends an email with a PDF attachment. 
     */ 
     public void email() { 
      String smtpHost = "103.12.134.112"; //replace this with a valid host 
      int smtpPort = 25; //replace this with a valid port 

      String sender = "[email protected]"; //replace this with a valid sender email address 
      String recipient = "[email protected]"; //replace this with a valid recipient email address 
      String content = "dummy content"; //this will be the text of the email 
      String subject = "dummy subject"; //this will be the subject of the email 

      Properties properties = new Properties(); 
      properties.put("mail.smtp.host", smtpHost); 
      properties.put("mail.smtp.port", smtpPort);  
      Session session = Session.getDefaultInstance(properties, null); 

      ByteArrayOutputStream outputStream = null; 

      try {   
       //construct the text body part 
       MimeBodyPart textBodyPart = new MimeBodyPart(); 
       textBodyPart.setText(content); 

       //now write the PDF content to the output stream 
       outputStream = new ByteArrayOutputStream(); 
       writePdf(outputStream); 
       byte[] bytes = outputStream.toByteArray(); 

       //construct the pdf body part 
       DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf"); 
       MimeBodyPart pdfBodyPart = new MimeBodyPart(); 
       pdfBodyPart.setDataHandler(new DataHandler(dataSource)); 
       pdfBodyPart.setFileName("test.pdf"); 

       //construct the mime multi part 
       MimeMultipart mimeMultipart = new MimeMultipart(); 
       mimeMultipart.addBodyPart(textBodyPart); 
       mimeMultipart.addBodyPart(pdfBodyPart); 

       //create the sender/recipient addresses 
       InternetAddress iaSender = new InternetAddress(sender); 
       InternetAddress iaRecipient = new InternetAddress(recipient); 

       //construct the mime message 
       MimeMessage mimeMessage = new MimeMessage(session); 
       mimeMessage.setSender(iaSender); 
       mimeMessage.setSubject(subject); 
       mimeMessage.setRecipient(Message.RecipientType.TO, iaRecipient); 
       mimeMessage.setContent(mimeMultipart); 

       //send off the email 
       Transport.send(mimeMessage); 

       System.out.println("sent from " + sender + 
         ", to " + recipient + 
         "; server = " + smtpHost + ", port = " + smtpPort);   
      } catch(Exception ex) { 
       ex.printStackTrace(); 
      } finally { 
       //clean off 
       if(null != outputStream) { 
        try { outputStream.close(); outputStream = null; } 
        catch(Exception ex) { } 
       } 
      } 
     } 

     /** 
     * Writes the content of a PDF file (using iText API) 
     * to the {@link OutputStream}. 
     * @param outputStream {@link OutputStream}. 
     * @throws Exception 
     */ 
     public void writePdf(OutputStream outputStream) throws Exception { 
      Document document = new Document(); 
      PdfWriter.getInstance(document, outputStream); 

      document.open(); 

      document.addTitle("Test PDF"); 
      document.addSubject("Testing email PDF"); 
      document.addKeywords("iText, email"); 
      document.addAuthor("Jee Vang"); 
      document.addCreator("Jee Vang"); 

      Paragraph paragraph = new Paragraph(); 
      paragraph.add(new Chunk("hello!")); 
      document.add(paragraph); 

      document.close(); 
     } 

     /** 
     * Main method. 
     * @param args No args required. 
     */ 
     public static void main(String[] args) { 
      SendAttachment demo = new SendAttachment(); 
      demo.email(); 
     } 
    } 

ERROR : 
    javax.mail.MessagingException: Could not connect to SMTP host: 103.12.134.112, port: 25; 
     nested exception is: 
     java.net.ConnectException: Connection timed out: connect 
     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:291) 
     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 com.airportbookingvn.SendAttachment.email(SendAttachment.java:89) 
     at com.airportbookingvn.SendAttachment.main(SendAttachment.java:136) 
    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:284) 
     at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227) 
     at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672) 
     ... 8 more 
+2

這是一個比編程問題更多的網絡問題。確保您嘗試聯繫的smtp服務器實際上正在應答該IP地址和端口。如果服務器是外部服務器並且您正在使用代理服務器,則端口25上的流量也可能會被阻止。 – DanielBarbarian 2014-08-29 08:38:05

回答

0

沒有SMTP服務器在此刻103.12.134.112端口25聽(我只是檢查)

您可以檢查使用telnet。請參閱balanv的回答https://stackoverflow.com/a/11988455/3536342瞭解更多信息。

+0

作爲您的建議,我運行telnet smtp.mydomain.com 25,我在命令提示符中出現此錯誤:連接到smtp.mydomain.com ...無法打開連接到主機,端口 25:連接失敗.. – 2014-08-29 09:46:35

相關問題