【发布时间】:2021-02-02 23:08:31
【问题描述】:
我有一个使用 Java Mail API 的发送电子邮件逻辑,当我在本地主机上时它工作正常,但一旦部署在谷歌云平台上,电子邮件会发送到我的 servlet,但从未送达。 我从 GoDaddy 购买了电子邮件,所以它是:xxx@mydomain.com。 在阅读了谷歌云平台上的文档和 StackOverflow 上的一些 cmets 后,我已经配置了防火墙规则,但似乎没有任何工作允许端口 25、465 和 587 上的入口和出口(我知道谷歌不允许端口 25 上的流量)。 我不想使用像 sendbird 这样的第 3 方电子邮件发件人......因为我之前使用过弹性并且我不需要第 3 方电子邮件发件人,JavaMail 就足够了。 所以我认为 Java 邮件对于 GCP 来说应该足够了。 谁能帮我吗? 这是我的发送电子邮件逻辑
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author sidibe ibrahim
* Sending email logic
*/
public class EmailSender {
static MessagingException me;
public static boolean sendMail(String from, String password, String message, String to[], String title) throws UnsupportedEncodingException {
String host = "smtpout.secureserver.net";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", password);
props.put("mail.smtp.host", 465);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.trust", "*");
Session session = Session.getDefaultInstance(props, null);
MimeMessage mimeMessage = new MimeMessage(session);
try {
mimeMessage.setFrom(new InternetAddress(from, "xxx"));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
mimeMessage.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
//sdd subject
mimeMessage.setSubject(title);
//set message to mimeMessage
mimeMessage.setText(message, "UTF-8", "html");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
return true;
} catch (MessagingException m) {
me.printStackTrace();
}
return false;
}
}
【问题讨论】:
标签: google-app-engine google-cloud-platform