Link to home
Start Free TrialLog in
Avatar of mkarthik415
mkarthik415

asked on

java mail

code for a simple to programe to send a mail using javamail api
ASKER CERTIFIED SOLUTION
Avatar of kadaba
kadaba
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Kin Fat SZE
Kin Fat SZE
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mkarthik415
mkarthik415

ASKER

Hi,
Thank you very much for your replies. I am using this code to send mail through java, but I have not received any mails. I have got no exceptions also.
TY


import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailWithPasswordAuthentication {
	public static void main(String[] args) throws MessagingException {
		new MailWithPasswordAuthentication().run();
	}

	private void run() throws MessagingException {
		Message message = new MimeMessage(getSession());

		message.addRecipient(RecipientType.TO, new InternetAddress("mkarthik415@gmail.com"));
		message.addFrom(new InternetAddress[] { new InternetAddress("mkarthik415@gmail.com") });

		message.setSubject("the subject");
		message.setContent("the body", "text/plain");
		System.out.println("message sent");

		Transport.send(message);
	}

	private Session getSession() {
		Authenticator authenticator = new Authenticator();

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
		properties.setProperty("mail.smtp.auth", "true");

		properties.setProperty("mail.smtp.host", "smtp.gmail.com");
		properties.setProperty("mail.smtp.port", "465");

		return Session.getInstance(properties, authenticator);
	}

	private class Authenticator extends javax.mail.Authenticator {
		private PasswordAuthentication authentication;

		public Authenticator() {
			String username = "mkarthik415";
			String password = "*******";
			System.out.println("selecting username and password");
			authentication = new PasswordAuthentication(username, password);
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return authentication;
		}
	}
}

Open in new window

After running the project with above code as java applicatioin I getting this exception

selecting username and password
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1694)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
      at javax.mail.Service.connect(Service.java:291)
      at javax.mail.Service.connect(Service.java:172)
      at javax.mail.Service.connect(Service.java:121)
      at javax.mail.Transport.send0(Transport.java:190)
      at javax.mail.Transport.send(Transport.java:120)
      at MailWithPasswordAuthentication.run(MailWithPasswordAuthentication.java:26)
      at MailWithPasswordAuthentication.main(MailWithPasswordAuthentication.java:14)
ty