Following is my code to send an email:
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "false");
final Session session = Session.getInstance(props);
final Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, toAddress);
msg.setSubject(subject);
msg.setSentDate(new Date());
Multipart multipart = new MimeMultipart("related");
BodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(body, "text/html");
multipart.addBodyPart(mbp1);
Transport.send(msg);
Error Stack trace:
javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:764)
at javax.mail.Session.getTransport(Session.java:689)
at javax.mail.Session.getTransport(Session.java:632)
at javax.mail.Session.getTransport(Session.java:612)
at javax.mail.Session.getTransport(Session.java:667)
at javax.mail.Transport.send0(Transport.java:154)
at javax.mail.Transport.send(Transport.java:80)
Note:
- Same code works if executed as a desktop application. But throws above exception when deployed on tomcat.
- Latest mail.jar and smtp.jar are added to library.
- SMTP host address is also correct.
If someone can give me pointers it will be helpful.