I am a newbie when it comes to java web programming. I tried doing a javamail code. here is my code
import java.util.Properties;..
public class SendEmail {
public void sendEmail(final CodeToEmailCommand emailSender) {
try {
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.trust", "*");
Session mailSession = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailSender.getUsername(), emailSender.getPassword());
}
});
mailSession.setDebug(true);
Message mailMessage = new MimeMessage(mailSession);
mailMessage.setFrom(new InternetAddress(emailSender.getFromEmail()));
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(emailSender.getToEmail()));
mailMessage.setSubject(emailSender.getSubject());
mailMessage.setContent(emailSender.getMessage(), "text/html");
mailMessage.setSubject(emailSender.getSubject());
Transport transport = mailSession.getTransport("smtp");
transport.connect("smtp.gmail.com", emailSender.getUsername(), emailSender.getPassword());
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
But it kept giving me this exception
DEBUG: setDebug: JavaMail version 1.4.4
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderExc
eption: unable to find valid certification path to requested target
..
I tried changing ports. Enabling firewall (in cmd) but it is still not working. Pls. Help. I am running out of ideas