amazon ec2 gmail smtp


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.NoSuchProviderException: 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.
public boolean sendMail(String email, String subject, String body, Integer port) {

    boolean sent = false;
    try {
        String host = "";
        String from = "";
        String pass = "password";
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true"); // added this line
        props.put("", 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);


        Multipart multipart = new MimeMultipart("related");
        BodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(body, "text/html");

        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());

        System.out.println("Mail Sent !! ");
        sent = true;
    } catch(Exception e) {
    return sent;

javax.mail.NoSuchProviderException: smtp
    at javax.mail.Session.getService(
    at javax.mail.Session.getTransport(
    at javax.mail.Session.getTransport(
    at javax.mail.Session.getTransport(
    at Mail.sendMail(
    at Mail.doGet(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    at org.apache.catalina.core.StandardWrapperValve.invoke(
    at org.apache.catalina.core.StandardContextValve.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.valves.ErrorReportValve.invoke(
    at org.apache.catalina.core.StandardEngineValve.invoke(
    at org.apache.catalina.connector.CoyoteAdapter.service(
    at org.apache.jk.server.JkCoyoteHandler.invoke(
    at org.apache.jk.common.HandlerRequest.invoke(
    at org.apache.jk.common.ChannelSocket.invoke(
    at org.apache.jk.common.ChannelSocket.processConnection(
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(
    at org.apache.tomcat.util.threads.ThreadPool$

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bryan ButlerCommented:
Do you have mail.jar in the classpath multiple times.  Changing to just one might fix it.
I wrote a java program to send stuff via gmail and it uses port 465. Your properties are a little off, try these instead:

props.put("mail.transport.protocol", "smtps");
props.put("", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.quitwait", "false");

..code attached.
import java.util.Properties;
import javax.mail.Session;
import javax.mail.AuthenticationFailedException;
import javax.mail.Transport;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;

public class GmailSender {

	private String SMTP_HOST_NAME = "";
	private int SMTP_HOST_PORT = 465;
	private String SMTP_AUTH_USER;
	private String SMTP_AUTH_PWD;
	private String SMTP_TO;
	private String SMTP_SUB;
	private String SMTP_BODY;

	public static void main(String[] args) throws Exception {
		if (args.length < 5){
			System.err.println("Usage: GmailSender <gmail-user-name> <gmail-password> <to> <subject> <file-body>");

		try {
			new GmailSender(args);
		} catch (AuthenticationFailedException e) {
			System.err.println("Your name/password is wrong, try again. <"+e.getMessage()+">");
		} catch (Exception e) {
			System.err.println("Problems: " + e.getMessage());

	public GmailSender(String[] args) throws Exception {
		SMTP_AUTH_USER = args[0];
		SMTP_AUTH_PWD = args[1];
		SMTP_TO = args[2];
		SMTP_SUB = args[3];
		SMTP_BODY = "";
		BufferedReader in = new BufferedReader(
			new InputStreamReader(
				new DataInputStream(
					new FileInputStream(args[4])
		String line;
		while ((line = in.readLine()) != null){ 
			SMTP_BODY += line + "\n";

		Properties props = new Properties();

		props.put("mail.transport.protocol", "smtps");
		props.put("", SMTP_HOST_NAME);
		props.put("mail.smtps.auth", "true");
		props.put("mail.smtps.quitwait", "false");

		Session mailSession = Session.getDefaultInstance(props);
		Transport transport = mailSession.getTransport();

		MimeMessage message = new MimeMessage(mailSession);
		message.setContent(SMTP_BODY, "text/html");

			 new InternetAddress(SMTP_TO));


		System.out.println("Successfully submitted email to " + SMTP_TO);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
world8Author Commented:

How do I check whether I've multiple jar files or not in the classpath ??
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Servers

From novice to tech pro — start learning today.