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.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 = "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)

Open in new window

world8Asked:
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.
0
jar3817Commented:
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("mail.smtps.host", 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;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;

public class GmailSender {

	private String SMTP_HOST_NAME = "smtp.gmail.com";
	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>");
			System.exit(1);
		}

		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";
		}
		in.close();

		Properties props = new Properties();

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

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

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

		message.addRecipient(Message.RecipientType.TO,
			 new InternetAddress(SMTP_TO));

		transport.connect
		  (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

		transport.sendMessage(message,
			message.getRecipients(Message.RecipientType.TO));
		transport.close();
		System.out.println("Successfully submitted email to " + SMTP_TO);
	}
}

Open in new window

0

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:
@developedtester

How do I check whether I've multiple jar files or not in the classpath ??
0
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.