2013-06-11 44 views
1

我无法执行公开可用于通过Gmail发送电子邮件的代码。我认为这可能是一个网络问题,因为我在工作,但我可以通过cmd提示符ping smtp.gmail.com来ping通Gmail。如何通过GMail使用SMTP时修复javax.mail.MessagingException?

这里是我使用(从this page)代码:

import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import java.util.Properties; 


class tester { 
    public static void main(String args[]) { 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.stmp.user", "username of the sender");   
     //If you want you use TLS 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.password", "password of the sender"); 
     // If you want to use SSL 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 
     Session session = Session.getDefaultInstance(props, new Authenticator() { 
      @Override 
       protected PasswordAuthentication getPasswordAuthentication() { 
        String username = "username"; 
        String password = "password"; 
        return new PasswordAuthentication("username","password"); 
       } 
      }); 
     String to = "[email protected]"; 
     String from = "[email protected]"; 
     String subject = "Testing..."; 
     MimeMessage msg = new MimeMessage(session); 
     try { 
      msg.setFrom(new InternetAddress(from)); 
      msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to)); 
      msg.setSubject(subject); 
      msg.setText("JAVA is the BEST"); 
      Transport transport = session.getTransport("smtp"); 
      transport.send(msg); 
      System.out.println("E-mail sent !"); 
     } catch(Exception exc) { 
      System.out.println(exc); 
     } 
    } 
} 

我已经改变了用户名和密码字段是正确的。有什么可能导致这种情况?我已经使用了我自己的代码和许多其他公开可用的代码,但是我得到了同样的错误。我也尝试通过端口587运行并收到相同的错误。

以下是错误:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; 
nested exception is: java.net.ConnectException: Connection refused: connect 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) 
    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) 
    at GoogleMail.sendMail(GoogleMail.java:45) 
+0

我无法使用telnet命令连接到SMTP服务器。 'telnet smtp.gmail.com 465'或'telnet smtp.gmail.com 587'我想这意味着我将无法使用Gmail作为SMTP服务器。我从哪里出发? – jeanqueq

回答

0

我在cmd提示使用的telnet smtp.gmail.com 465telnet smtp.gmail.com 587命令,并且无法连接。我假设它与我连接的网络有关。除此之外,我无法提供任何信息,如果我了解详情,我会更新。