2016-04-08 130 views
2

我試圖通過Gmail smtp服務器通過Java代碼來發送郵件,但面臨java.net.SocketException: Connection Resetjava.net.SocketException異常而通過Gmail SMTP服務器通過Java發送郵件

以下是發送郵件代碼:-`

Properties props = new Properties(); 
    props.put("mail.smtp.host",host); //host is smtp.gmail.com 
    if(host.equalsIgnoreCase("smtp.gmail.com")) 
    { 
     props.put("mail.smtp.socketFactory.port",port); 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port",port);//port is 465 

    } 
    else 
    { 
     props.put("mail.smtp.auth", "true"); 
    } 
    Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(uname,pwd); 
     } 
    }); 

     try { 

      session.setDebug(true); 
      MimeMessage message = new MimeMessage(session); 

      message.setFrom(new InternetAddress(uname)); 

      message.addRecipient(Message.RecipientType.TO,new InternetAddress(toMail)); 

      message.setSubject(NewFolderName); 
      System.out.println("Composing message body."); 

      StringBuilder htmlStreamBuilder = new StringBuilder(); 
      // we create some html string here..... 
      Multipart multipart = new MimeMultipart(); 

      MimeBodyPart htmlPart = new MimeBodyPart(); 
      MimeBodyPart attachmentPart= new MimeBodyPart();    

      DataSource source = new FileDataSource(filename); 
      attachmentPart.setDataHandler(new DataHandler(source)); 
      attachmentPart.setFileName(KMAConstants.TESTNG_ATTACHMENT_FILE); 

      htmlPart.setContent(htmlStream, "text/html; charset=utf-8");//we give some html stream here .... 


      multipart.addBodyPart(htmlPart); 
      multipart.addBodyPart(attachmentPart); 


      message.setContent(multipart); 

      Transport.send(message); 



     } catch (MessagingException e) 
     { 

      System.out.println("Issue in message sending, MessagingExceptionn raised."); 
      e.printStackTrace(); 

      throw e; 
     } 
    } 

    ` 

它引發以下錯誤: - `

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false DEBUG SMTP: exception reading response, THROW: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:209) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read(BufferedInputStream.java:265) at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89) at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2184) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1939) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) 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 com.rsi.kma.common.utils.KmaUtil.sendMailNow(KmaUtil.java:1907) at com.rsi.kma.ui.testconfigureui.FileProcessingTask.call(FileProcessingTask.java:401) at com.rsi.kma.ui.testconfigureui.FileProcessingTask.call(FileProcessingTask.java:1) at javafx.concurrent.Task$TaskCallable.call(Task.java:1423) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.lang.Thread.run(Thread.java:745) Issue in message sending, MessagingExceptionn raised.

`

這裏需要注意的一點很重要,這段代碼適用於某些計算機。

請幫我, 在此先感謝。

+0

有些電腦在同一網絡?相同的Java版本? – mauros

+0

是的,所有的電腦都在同一個vlan上。所有使用相同的Java 8 build 60版本。 –

+0

而在這些計算機上這個代碼總是工作? – mauros

回答

1

網絡策略阻止了對SMTP服務器的請求。我聯繫了我的網絡管理員,他給了我必要的權限。現在一切正常。

相關問題