2013-03-13 174 views
15

在发送电子邮件我得到这个错误无法连接到SMTP主机:smtp.gmail.com端口:465,响应:-1

java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

我的代码是:

Properties props = new Properties(); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.socketFactory.port", "465"); 
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","password"); 
       } 
     }); 

try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("email")); 
     message.setRecipients(Message.RecipientType.TO, 
         InternetAddress.parse(this.to)); 
     message.setSubject("Testing"); 
     message.setText("Hey, this is the testing email."); 



     Transport.send(message); 

任何帮助,将不胜感激。

在此先感谢。

+0

检查http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ – Abi 2013-03-13 06:14:53

+0

我试过相同的代码,但仍然得到错误:javax.mail.MessagingException :530 5.7.0必须首先发出STARTTLS命令。 4sm28216799pbn.23 - gsmtp \t在com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)在com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716) \t在COM。 sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388) \t at rapid.mail.main(mail.java:60)starttls的这一行是那里,那么为什么得到相同的错误:properties.put(“mail .smtp.starttls.enable“,”true“); – user1900376 2013-03-13 06:23:11

+0

尝试使用最新的jar版本JavaMail 1.4.7 – Abi 2013-03-13 06:36:11

回答

11

你需要告诉它你使用SSL:

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 

如果你错过了什么,这里是工作代码:

String d_email = "[email protected]", 
      d_uname = "Name", 
      d_password = "urpassword", 
      d_host = "smtp.gmail.com", 
      d_port = "465", 
      m_to = "[email protected]", 
      m_subject = "Indoors Readable File: " + params[0].getName(), 
      m_text = "This message is from Indoor Positioning App. Required file(s) are attached."; 
    Properties props = new Properties(); 
    props.put("mail.smtp.user", d_email); 
    props.put("mail.smtp.host", d_host); 
    props.put("mail.smtp.port", d_port); 
    props.put("mail.smtp.starttls.enable","true"); 
    props.put("mail.smtp.debug", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.socketFactory.port", d_port); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 

    SMTPAuthenticator auth = new SMTPAuthenticator(); 
    Session session = Session.getInstance(props, auth); 
    session.setDebug(true); 

    MimeMessage msg = new MimeMessage(session); 
    try { 
     msg.setSubject(m_subject); 
     msg.setFrom(new InternetAddress(d_email)); 
     msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to)); 

Transport transport = session.getTransport("smtps"); 
      transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password); 
      transport.sendMessage(msg, msg.getAllRecipients()); 
      transport.close(); 

     } catch (AddressException e) { 
      e.printStackTrace(); 
      return false; 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
      return false; 
     } 
+0

我得到异常。你能告诉我它失败的位置吗 javax.mail.SendFailedException:发送失败; 嵌套的异常是: \t class javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587; 嵌套的异常是: \t java.net.ConnectException:连接超时:连接 \t at javax.mail.Transport.send0(Transport。的java:218) \t在javax.mail.Transport.send(Transport.java:80) \t在sendMail.SendMail4.execute(SendMail4.java:70) \t在sendMail.sel.main(sel.java:10 ) – 2016-02-23 10:58:39

+0

谢谢。我不知道'd_uname'字段实用程序是什么。另一方面,在连接指令中,我们应该传递电子邮件而不是用户名:'transport.connect(d_host,Integer.valueOf(d_port),d_email,d_password);'。另外,你最好从代码中的'SMTPAuthenticator'类中显示包,或者在答案中指定所需的API。 – Omar 2016-04-16 19:46:36

5

我有在使用NetBeans进行调试时遇到此问题,甚至执行实际的jar文件。防病毒会阻止发送电子邮件。您应该在调试期间临时禁用您的防病毒软件,或者排除NetBeans和实际的jar文件被扫描。就我而言,我正在使用Avast。

参见如何排除此链接:How to Add File/Website Exception into avast! Antivirus 2014

这对我的作品。

+0

您能否更详细地描述SMTP响应和防病毒之间的连接? – carlodurso 2014-10-25 18:39:43

+0

谢谢 - Avast也造成了我的'问题'... – DaniEll 2015-06-18 17:20:53

+1

@carlodurso:防病毒,(在我的情况下,AVAST)具有阻止程序的功能,这些程序使用端口进出的怀疑是安全威胁。所以最好排除你的jar文件被阻止。 – ronIT 2015-10-21 20:36:40

1

我做什么我注释掉

props.put("mail.smtp.starttls.enable","true"); 

因为显然是G-邮件,你并不需要它。如果你还没有这样做,你需要在你的程序的G-mail中创建一个应用程序密码。我做到了这一点,它的工作完美。这里的这个链接会告诉你如何:https://support.google.com/accounts/answer/185833

相关问题