简体   繁体   English

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

[英]Sending mail on localhost works, but when deployed to webserver I get Couldn't connect to host, port: smtp.gmail.com, 587

I know this question is all over the stack overflow but I couldn-t find any answer that works.我知道这个问题到处都是堆栈溢出,但我找不到任何有效的答案。

I'm trying to send mail from my webApp to gmail account (like contact form) and as it's stated in the title, sending mail on localhost works, but when deployed to webserver I get我正在尝试将邮件从我的 webApp 发送到 gmail 帐户(如联系表格),正如标题中所述,在本地主机上发送邮件有效,但是当部署到网络服务器时,我得到了

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)

I have freshly installed Ubuntu on Linode VPS running Tomcat 9.我在运行 Tomcat 9 的 Linode VPS 上新安装了 Ubuntu。

ufw (firewall) is disabled ufw(防火墙)被禁用

On gmail account I've enabled在 gmail 帐户上我已启用

  • Less secure app access (it wasn-t working on localhost before this option was enabled)不太安全的应用程序访问(在启用此选项之前它无法在 localhost 上运行)
  • POP3 POP3
  • IMAP地图

I've also tried enabling 2 factor auth and genereting app password on gmail.我还尝试在 gmail 上启用 2 因素身份验证和生成应用程序密码。 Again it worked on, localhost but not when deployed on tomcat.它再次在 localhost 上运行,但在部署在 tomcat 上时不起作用。

My code for sending mail:我发送邮件的代码:

   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);
   }
   
   
}

Also I've tried adding this but it didn't help我也试过添加这个,但没有帮助

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);

I suppose that I have to enable something on Ubuntu or Tomcat, but I'm not sure what我想我必须在 Ubuntu 或 Tomcat 上启用某些东西,但我不确定是什么

Ok, I've found a solution.好的,我找到了解决方案。

First - Linode VPS (and digital ocean droplets too from what I've read) have restrictions by default on smpt traffic in order to help combat spam on their platform.首先 - Linode VPS(以及我读过的数字海洋水滴)默认情况下对 smpt 流量有限制,以帮助打击其平台上的垃圾邮件。 So you have to contact them through support and ask them to lift those restrictions for your VPS所以你必须通过支持联系他们,并要求他们解除对你的 VPS 的限制

Second - after allowing less secure apps (or setting up app apsword through gmail) you have to go to https://accounts.google.com/DisplayUnlockCaptcha and click Continue.第二 - 在允许不太安全的应用程序(或通过 gmail 设置应用程序 apsword)后,您必须将 go 到https://accounts.google.com/DisplayUnlockCaptcha并单击继续。 Basically allowing connection from your app.基本上允许从您的应用程序连接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.apache.commons.mail.EmailException:将电子邮件发送到以下服务器失败:smtp.gmail.com:465 - org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465 javax.mail.MessagingException:无法通过netbeans在本地连接到SMTP主机 - javax.mail.MessagingException: Could not connect to SMTP host locally via netbeans 更改主机公共IP的localhost时无法访问servlet - I can't access a servlet when changing localhost for the host public IP 无法在浏览器[docker]中公开从本地主机访问Web服务器的端口 - Cannot expose port to access webserver from localhost in browser [docker] 第二次如何连接smtp端口25? - How to connect smtp port 25 in second time? 从 gmail 发送电子邮件有效,但不是 yahoo - Sending email works from gmail but not yahoo 我的 tomcat 正在运行,但无法连接到 http://localhost:8080 - My tomcat is running but I can't connect to http://localhost:8080 localhost:8080 / lio /在我的apachtomcat中效果很好。 但是当我放 <my ip> :8080 / lio /我收到错误 - localhost:8080/lio/ works great in my apachtomcat. but when i put <my ip>:8080/lio/ i get error 在部署到tomcat的GWT Servlet中使用Java sendmail发送邮件 - sending mail with java sendmail in GWT servlet deployed to tomcat 无法将设备连接到本地主机 - Can't connect device to localhost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM