world8
asked on
amazon ec2 gmail smtp
Hi,
I'm trying to send a mail using google apps smtp on my amazon ec2 instance.
I've tried send the mail using ports: 25 & 587, but no luck I get the "javax.mail.NoSuchProvider Exception: smtp" error: The same code is working fine from my localsystem.
Please let me know what should I do for the mail to be sent.
I'm trying to send a mail using google apps smtp on my amazon ec2 instance.
I've tried send the mail using ports: 25 & 587, but no luck I get the "javax.mail.NoSuchProvider
Please let me know what should I do for the mail to be sent.
public boolean sendMail(String email, String subject, String body, Integer port) {
boolean sent = false;
try {
String host = "smtp.gmail.com";
String from = "user@domain.com";
String pass = "password";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from, "Name"));
InternetAddress toAddress = new InternetAddress(email);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
Multipart multipart = new MimeMultipart("related");
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(body, "text/html");
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Mail Sent !! ");
sent = true;
} catch(Exception e) {
e.printStackTrace();
}
return sent;
}
javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:782)
at javax.mail.Session.getTransport(Session.java:708)
at javax.mail.Session.getTransport(Session.java:651)
at javax.mail.Session.getTransport(Session.java:631)
at Mail.sendMail(Mail.java:67)
at Mail.doGet(Mail.java:35)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:200)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:773)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:703)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:895)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:675)
Do you have mail.jar in the classpath multiple times. Changing to just one might fix it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@developedtester
How do I check whether I've multiple jar files or not in the classpath ??
How do I check whether I've multiple jar files or not in the classpath ??