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);
that way just not works
MailEngine.send(from, to, sub, body);
that way gives me that Error:
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
What should I do to make it work?
to send email from Liferay I recommend the use of the Message Bus. There are two destination ad hoc for this purpose. You can find a complete example on my GitHub repository. A follow a piece of sample code that uses the class SubscriptionSender which in turn makes use of the Message Bus.
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();
You can find the complete source at SendMail.java
您缺少身份验证属性,请查看此示例以获取完整示例。
Most likely the problem is you are connecting using the SMTP protocol without SSL. If this is JavaMail-based, make sure you use the smtps protocol as that is SMTP with SSL. Also make sure you enable authentication. See the JavaMail FAQ entry on GMail
Did you check "Use a Secure Network Connection" under "Outgoing SMTP Server" on mail configuration page?
You could also try setting "Manually specify additional JavaMail properties to override the above configuration." to
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
Here is a GMail configuration in portal-ext.properties that worked for me for years:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.