2016-02-07 38 views
0

我想發送一個電子郵件在Windows 8.1與java 1.8使用javax.mail API,但我不斷收到錯誤。未能發送郵件與Javax.mail

這裏是我寫的方法:

public void sendAlertMail(String from, String to, String header, String msgBody){ 
     Properties props = new Properties(); 
     props.put("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.host", smptHost); 
     props.put("mail.smtp.port", smptPort); 
     props.put("mail.smtp.starttls","true"); 
     Session mailSession; 
     if (authenticate) { 
      props.put("mail.smtp.auth", "true"); 

      Authenticator auth = new SMTPAuthenticator(); 
      mailSession = Session.getInstance(props, auth); 
     } 
     else{ 
      mailSession = Session.getInstance(props); 
     } 
     // uncomment for debugging infos to stdout 
     // mailSession.setDebug(true); 
     Transport transport = null; 
     try { 
      transport = mailSession.getTransport(); 
     } catch (NoSuchProviderException e) { 
      e.printStackTrace(); 
     } 

     MimeMessage message = new MimeMessage(mailSession); 
     try { 
      message.addHeaderLine(header); 
      message.setContent(msgBody, "text/plain"); 
      message.setFrom(new InternetAddress(from)); 
      message.addRecipient(Message.RecipientType.TO, 
        new InternetAddress(to)); 
     } catch (MessagingException e) { 
     } 
     try { 
      transport.connect(); 
      transport.sendMessage(message, 
        message.getRecipients(Message.RecipientType.TO)); 
      transport.close(); 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
     } 
    } 

我收到的錯誤是:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1; 
    nested exception is: 
    java.net.SocketException: Permission denied: 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:388) 
    at javax.mail.Service.connect(Service.java:246) 
    at javax.mail.Service.connect(Service.java:195) 
    at tapperSolution.mail.MailSender.sendAlertMail(MailSender.java:64) 
    at tapperSolution.mail.MailSender.sendAlertMail(MailSender.java:74) 
    at tapperSolution.mail.MailSender.main(MailSender.java:88) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 
Caused by: java.net.SocketException: Permission denied: connect\at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
at java.net.Socket.connect(Socket.java:589) 
at java.net.Socket.connect(Socket.java:538) 
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) 
... 12 more 

我關掉我的防火牆和防病毒,我試圖設置在VM選項我使用代碼:

System.setProperty("java.net.preferIPv4Stack", "true"); 

還試圖與設置它在運行命令:

-Djava.net.preferIPv4stack =真

我能夠給我使用Python的郵件,但我想用Java來做到這一點。

試圖捕獲使用Wireshark的流量,但是甚至沒有與服務器的TCP握手。

還有其他想法嗎?

謝謝

+0

mail.smtp.starttls.enable = true?你設置了那個標誌嗎? –

+0

嘗試在您的應用程序之外使用telnet連接到電子郵件並查看會發生什麼。如果你可以連接,那麼它是你的應用程序的權限,需要看,而不是防火牆問題 – DaveRlz

+0

你正在運行一個獨立的應用程序,一個小程序,一個jnlp應用程序或其他? –

回答

0

它看起來像您的SMTP服務器可能會拒絕您的連接,可能是由於身份驗證問題。要測試它,請嘗試使用來自同一臺計算機的manual SMTP session,並查看結果是什麼。

+0

看起來訪問在本地被拒絕,因爲服務器沒有流量。我能夠使用Python將來自同一臺計算機的電子郵件發送到同一臺服務器。 – AndreyF