2014-07-03 270 views
2

我正尝试在java中编写一个应用程序来发送电子邮件,我在YouTube上找到了一个教程并试图关注它。但它不适合我还在上班,这里是错误我得到JavaMail无法发送电子邮件

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: 421 
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1949) 
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) 
at javax.mail.Service.connect(Service.java:295) 
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 libraryFineList.ParseRecords.main(ParseRecords.java:90) 

我不知道,什么是错的,什么是我在谷歌发现,没有帮助,

这里是代码

public static void main(String[] args) throws IOException, MessagingException { 

    Properties props = new Properties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    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 javax.mail.Authenticator(){ 
     protected PasswordAuthentication getPasswordAuthentication(){ 
     return new PasswordAuthentication("[email protected]", "pass"); 

     } 
    } 

      ); 

    try{ 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.addRecipient(Message.RecipientType.TO, 
        new InternetAddress("[email protected]", "Mr. User")); 
     message.setSubject("Your Example.com account has been activated"); 
     message.setText("Worked"); 
    Transport.send(message); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

here are the libraries that i downloaded and added to build path

enter image description here

+0

为我工作(javax.mail 1.5.2)。当然你没有防火墙或路由器问题? – MadProgrammer

+1

你是否也考虑尝试一个新的端口587例如,但错误消息并不真正匹配你的源代码 – MadProgrammer

+0

@MadProgrammer刚刚尝试过的错误是完全一样的,我可以用防火墙或路由器有什么样的问题? – Jenny

回答

0

我t很奇怪,你的错误信息是抱怨连接到localhost的问题:25当你的属性文件明确提示你应该使用smtp.gmail.com。

我不认为有什么可怕的事情与您的主机文件重定向smtp.gmail.com回到127.0.0.1什么的,是吗?如果您从命令行输入ping smtp.gmail.com,会发生什么情况?

除此之外,我建议检查您使用的是最新版本的Java邮件。

+0

发布ping结果 – Jenny