开发者社区> 问答> 正文

用google邮箱 的 smtp.gmail.com 服务器发送邮件失败?报错

由于业务需要的原因,现在需要用google 邮箱发送邮件,现在总是连接不上,我实在不知道是哪里的问题了,求助于各位同行。

我的测试环境 :window10    jdk1.7   VPN (连接外网没有问题) 

 下面是我的代码: 

发送:

public class GmailSender {
 
 public static void main(String[] args) throws Exception {
  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  Properties props = System.getProperties();
  props.setProperty("mail.smtp.host", "smtp.gmail.com");
  props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
  props.setProperty("mail.smtp.socketFactory.fallback", "false");
  props.setProperty("mail.smtp.port", "465");
  props.setProperty("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.auth", "true");
  final String username = "kingXXXXbao@gmail.com";
  final String password = "XXXXXX";
  Session session = Session.getDefaultInstance(props, new Authenticator(){
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(username, password);
      }});
  Message msg = new MimeMessage(session);
  msg.setFrom(new InternetAddress(username));
  msg.setRecipients(Message.RecipientType.TO, 
    InternetAddress.parse("XXXX308@qq.com",false));
  msg.setSubject("Hello");
  msg.setText("How are you");
  msg.setSentDate(new Date());
  Transport.send(msg);
  System.out.println("Message sent.");
 }
}

报错如下:

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
	java.net.ConnectException: Connection timed out: connect
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
	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 Gmail.GmailSender.main(GmailSender.java:40)
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:579)
	at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625)
	at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160)
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:207)
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
	... 7 more

我的google 邮箱的stmp 的设置如下,不知道哪里有问题,求助:

 

 

 

展开
收起
爱吃鱼的程序员 2020-06-07 17:31:13 8870 0
2 条回答
写回答
取消 提交回答
  • image.png 把smtp.gmail.com换成173.194.193.108 试试?参考https://js.aizhan.com/pc/zhishi/6641.html

    2020-07-31 17:15:11
    赞同 展开评论 打赏
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>已知被郭嘉防火墙拦住了。望采纳!</p>
    
                    
    
                            回复 <a class="referer" target="_blank">@圣笑知</a>  : 后来没有搞了
                        
    
                            回复 <a class="referer" target="_blank">@wangyanbao</a>  : 你好,你这个问题现在解决了没有?我也遇到这个问题了
                        
    
                            我用vpn 是可以登录google meil的
                        
    
                            我用了vpn 的,难道不行吗?
    
    2020-06-07 17:31:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS块储存产品全面解析 立即下载