[英]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 帐户上我已启用
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.