首页 > 解决方案 > 在本地主机上发送邮件有效,但是当部署到网络服务器时,我得到无法连接到主机,端口:smtp.gmail.com,587

问题描述

我知道这个问题到处都是堆栈溢出,但我找不到任何有效的答案。

我正在尝试将邮件从我的 webApp 发送到 gmail 帐户(如联系表格),正如标题中所述,在 localhost 上发送邮件有效,但是当部署到网络服务器时,我得到了

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1; nested exception is: java.net.ConnectException: Connection timed out (Connection timed out)

我在运行 Tomcat 9 的 Linode VPS 上新安装了 Ubuntu。

ufw(防火墙)被禁用

在我已启用的 gmail 帐户上

我还尝试在 gmail 上启用 2 因素身份验证和生成应用程序密码。它再次在 localhost 上工作,但在部署在 tomcat 上时却不行。

我发送邮件的代码:

   Properties props = new Properties();
   props.put("mail.transport.protocol", "smtp");
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.starttls.enable", "true");
   props.put("mail.smtp.starttls.required", "true");
   props.put("mail.smtp.host", "smtp.gmail.com");
   props.put("mail.smtp.port", "587"); //I''ve tried with port 25 also..it didn't work
  
   Session session = Session.getInstance(props, new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication("myMail@gmail.com", "myPassword"); 
         
      }
   });
   Message msg = new MimeMessage(session);
   msg.setFrom(new InternetAddress(mailRequest.getEmail(), mailRequest.getName())); //get email ne radi radi google maila
    
   msg.setReplyTo(new InternetAddress[]{new InternetAddress(mailRequest.getEmail())});
   msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("myMail@gmail.com"));
   msg.setSubject(mailRequest.getSubject());
   msg.setContent(mailRequest.getMessage(), "text/html");
   msg.setSentDate(new Date());

   MimeBodyPart messageBodyPart = new MimeBodyPart();
   messageBodyPart.setContent(mailRequest.getMessage(), "text/html");

   Multipart multipart = new MimeMultipart();
   multipart.addBodyPart(messageBodyPart);

   msg.setContent(multipart);
   Transport.send(msg);
   
   if (mailRequest.isSendCopyToUser()) {
       Message msgForUser = new MimeMessage(session);
       msgForUser.setFrom(new InternetAddress("myMail@gmail.com", "Galerija Lemaić")); 
        
       msgForUser.setReplyTo(new InternetAddress[]{new InternetAddress("myMail@gmail.com")});
       msgForUser.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailRequest.getEmail()));
       msgForUser.setSubject(mailRequest.getSubject());
       msgForUser.setContent(mailRequest.getMessage(), "text/html");
       msgForUser.setSentDate(new Date());

       MimeBodyPart messageuserBodyPart = new MimeBodyPart();
       messageuserBodyPart.setContent(mailRequest.getMessage(), "text/html");

       Multipart multipartUser = new MimeMultipart();
       multipartUser.addBodyPart(messageBodyPart);
       msgForUser.setContent(multipartUser);
       Transport.send(msgForUser);
   }
   
   
}

我也试过添加这个,但没有帮助

props.put("mail.store.protocol", "pop3s");
props.put("mail.pop3.host", "pop.gmail.com");     
props.put("mail.pop3.user", "myMail@gmail.com");
props.put("mail.pop3.socketFactory", 995);
props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.port", 995);

我想我必须在 Ubuntu 或 Tomcat 上启用某些东西,但我不确定是什么

标签: tomcatsmtpgmailtimeouthost

解决方案


好的,我找到了解决方案。

首先 - Linode VPS(以及我读过的数字海洋水滴)默认情况下对 smpt 流量有限制,以帮助打击其平台上的垃圾邮件。所以你必须通过支持联系他们,并要求他们解除对你的 VPS 的限制

其次 - 在允许不太安全的应用程序(或通过 gmail 设置应用程序 apsword)后,您必须转到https://accounts.google.com/DisplayUnlockCaptcha并单击继续。基本上允许从您的应用程序连接。

关于第二点。这是解决方案 javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1


推荐阅读