javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587; java.net.NoRouteToHostException:没有到主机的路由

javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587; java.net.NoRouteToHostException:没有到主机的路由

问题描述:

我尝试了很多的教程创建的电子邮件发送程序,但每一个代码抛出了同样的错误:javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587; java.net.NoRouteToHostException:没有到主机的路由

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.NoRouteToHostException: No route to host (Host unreachable)

我使用Ubuntu 16.04和Eclipse Neon.1a版本(4.6.1)

这是我的代码:

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.mail.internet.MimeMessage; 

public class SendEmail { 

public static void main(String args[]){ 
    try{ 
     String host ="smtp.gmail.com" ; 
     String user = "[email protected]"; 
     String pass = "myPassword"; 
     String to = "[email protected]"; 
     String from = "[email protected]"; 
     String subject = "My first email"; 
     String messageText = "Some text"; 
     boolean sessionDebug = false; 

     Properties props = System.getProperties(); 

     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.port", "587"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.required", "true"); 

     //java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
     Session mailSession = Session.getDefaultInstance(props, null); 
     mailSession.setDebug(sessionDebug); 
     Message msg = new MimeMessage(mailSession); 
     msg.setFrom(new InternetAddress(from)); 
     InternetAddress[] address = {new InternetAddress(to)}; 
     msg.setRecipients(Message.RecipientType.TO, address); 
     msg.setSubject(subject); msg.setSentDate(new Date()); 
     msg.setText(messageText); 

     Transport transport=mailSession.getTransport("smtp"); 
     transport.connect(host, user, pass); 
     transport.sendMessage(msg, msg.getAllRecipients()); 
     transport.close(); 
     System.out.println("message send successfully"); 
    }catch(Exception ex) 
    { 
     System.out.println(ex); 
    } 
}} 

难道我也许需要安装额外的软件,还是其他什么东西?

UPDATE:

的问题是在我的无线路由器的地方。

我在手机上通过热点使用移动(3G)互联网,一切都很顺利。

现在我们可以解决问题,如果有人有路由器设置的经验。

+0

看来你不能得到达到谷歌的邮件服务器,你可以从'平smtp.gmail.com'你的电脑? –

+0

可能是客户端计算机上的防火墙,阻止传出呼叫或在另一端的某处。详细信息可以在这里找到,https://coderanch.com/t/476146/java/SMTP-Send-Email-Java-Mail –

+0

对不起,但我不太明白,'ping smtp.gmail是什么意思。 com'? – elano7

它可能是阻止拨出呼叫的防火墙。防火墙可以在客户端或服务器上。

+0

和我能做些什么来避免这种情况?我试过这个命令'ping smtp.gmail.com',并得到这个结果: 'PING gmail-smtp-msa.l.google.com(74.125.133.108)56(84)字节的数据。来自wo-in-f108.1e100.net(74.125.133.108)的64个字节:icmp_seq = 1 ttl = 41时间= 56.5ms --- gmail-smtp-msa.l.google.com ping统计数据---发送了25个数据包,25收到,0%丢包率,时间24034ms rtt min/avg/max/mdev = 55.846/72.355/100.889/11.747 ms – elano7

+0

问题出在我的无线路由器的某个地方。 我在手机上通过热点使用移动(3G)互联网,一切都很顺利。 现在我们可以解决问题,如果有人有路由器设置的经验。 – elano7

+0

什么路由器?所有路由器都不同。你自己设置了路由器吗? “不小心”设置防火墙的可能性不大。您确定您的计算机上没有防火墙或防病毒软件阻止访问?JavaMail FAQ有[连接调试技巧](https://javaee.github.io/javamail/FAQ#condebug)。 –

可能对第三方您的谷歌应用程序的授权是关闭的,所以让他们尝试: https://myaccount.google.com/lesssecureapps

+0

问题出在我的无线路由器的某个地方。 我在手机上通过热点使用移动(3G)互联网,一切都很顺利。 现在我们可以解决问题,如果有人有路由器设置的经验。 – elano7