try {
InternetAddress to = new InternetAddress("tehilar20@gmail.com");
InternetAddress from = new InternetAddress("tehilar20@gmail.com");
String sub = new String("test");
String body = new String("test-mail");
MailMessage msg = new MailMessage();
msg.setTo(to);
msg.setFrom(from);
msg.setBody(body);
msg.setSubject(sub);
MailServiceUtil.sendEmail(msg);
MailEngine.send(from, to, sub, body);
} catch (Exception e) {
System.out.println("can't send mail");
}
MailServiceUtil.sendEmail(msg);
这种方式不起作用
MailEngine.send(from, to, sub, body);
这种方式给了我错误:
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
我该怎么做才能让它发挥作用?
答案 0 :(得分:1)
您缺少身份验证属性,请查看this以获取完整示例。
答案 1 :(得分:1)
问题很可能是您使用没有SSL的SMTP协议进行连接。如果这是基于JavaMail的,请确保使用smtps协议,因为它是带SSL的SMTP。还要确保启用身份验证。请参阅JavaMail FAQ entry on GMail
答案 2 :(得分:1)
您是否在邮件配置页上的“外发SMTP服务器”下选中了“使用安全网络连接”?
您还可以尝试设置“手动指定其他JavaMail属性以覆盖上述配置”。到
mail.smtp.auth=true
mail.smtp.port=465
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
答案 3 :(得分:1)
从Liferay发送电子邮件我建议使用消息总线。为此目的,有两个目的地临时。您可以在我的GitHub存储库中找到完整的示例。按照一段使用SubscriptionSender类的示例代码,该类继续使用消息总线。
SubscriptionSender subscriptionSender = new SubscriptionSender();
subscriptionSender.setSubject("Lista degli utenti disattivati");
subscriptionSender.setBody(emailBody.toString());
subscriptionSender.setUserId(user.getUserId());
subscriptionSender.setCompanyId(user.getCompanyId());
subscriptionSender.setFrom("noreply@liferay.com", "Liferay Portal");
subscriptionSender.setHtmlFormat(false);
subscriptionSender.setMailId("user", user.getUserId());
subscriptionSender.addRuntimeSubscribers(
user.getEmailAddress(),
user.getFullName());
List<EmailAddress> emails = (List<EmailAddress>) user.getEmailAddresses();
if (emails.size() > 0) {
if (_log.isInfoEnabled()) {
_log.info("User " + user.getUserId() +
" has additional emails address");
}
for (EmailAddress emailAddress : emails) {
subscriptionSender.addRuntimeSubscribers(
emailAddress.getAddress(),
(String) user.getFullName());
}
}
subscriptionSender.flushNotificationsAsync();
您可以在SendMail.java
找到完整的来源答案 4 :(得分:0)
以下是portal-ext.properties中的GMail配置,它对我有用多年:
mail.session.mail.pop3.host=localhost
mail.session.mail.pop3.password=
mail.session.mail.pop3.port=110
mail.session.mail.pop3.user=
mail.session.mail.smtp.auth=true
mail.session.mail.smtp.host=smtp.gmail.com
mail.session.mail.smtp.port=465
mail.session.mail.smtp.password=<MY-PASSWORD-ON-GMAIL>
mail.session.mail.smtp.user=<MY-USERNAME-ON-GMAIL-(WITHOUT-@GMAIL.COM)>
mail.session.mail.store.protocol=pop3
mail.session.mail.transport.protocol=smtp
mail.session.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.session.mail.smtp.starttls.enable=true