Reputation: 4562
I am trying to send an email notification via Gmail SMTP server using SSL connection. But no email is getting. Even no error is reported there.My method send()
is giving below.Please help.I am using JSF2 .
public void send()
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("userName","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
Upvotes: 3
Views: 4027
Reputation: 29971
This code is overly complex. The JavaMail FAQ has sample code for using Gmail. It also has tips for debugging your problems.
One particular issue is that you should use Session.getInstance instead of Session.getDefaultInstance.
Upvotes: 2