tomcat - 在本地主机上发送邮件有效,但是当部署到网络服务器时,我得到无法连接到主机,端口: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 帐户上
- 不太安全的应用程序访问(在启用此选项之前它不能在 localhost 上运行)
- POP3
- 地图
我还尝试在 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 上启用某些东西,但我不确定是什么
解决方案
好的,我找到了解决方案。
首先 - 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
推荐阅读
- django - 如何正确查询 manyTOmany 模型字段?
- ssh - 寻求在服务器端充当终端的 SSH 模拟器
- ibm-integration-bus - 如何在 IIB Toolkit 的映射节点中提交每个 DB 事务?
- php - 用AJAX更新商品数量有问题
- java - xpath 在 XML 中获取子节点相对于父节点的位置
- javascript - Joi - 是否可以引用多个值?
- latex - Latex 数学模式中的字间距
- julia - Julia中具有相同变量类型的多次调度
- python - 是什么解释了 Dask 在追加多列时 DataFrame.assign(**kwargs) 和 dd[x]=y 之间的性能差异?
- asp.net-mvc - 在 Asp.Net 中实现多对多关系(困惑?)