Press enter to see results or esc to cancel.

Java program to send email using smtp

Download the project shown in youtube here,

  1. Project Structure in Eclipse.
  2. This project uses jars activation-1.1.1.jaractivation-1.1.1.jar and mail-1.4.1.jar.mail-1.4.1.jar
  3. Send email using Gmail with Java program below
    Update the From email id,password and to email id with your email information.

    package email;
    import java.util.*; 
    import javax.mail.*; 
    import javax.mail.PasswordAuthentication;
    import javax.mail.internet.*;
    public class send_email_gmail {
    	public static void main(String[] args) {
    		final String username = "";
    		final String password = "password";
    		Properties props = new Properties();
    		props.put("mail.smtp.auth", "true");
    		props.put("mail.smtp.starttls.enable", "true");
    		props.put("", "");
    		props.put("mail.smtp.port", "587");
    		Session session = Session.getInstance(props,
    		  new javax.mail.Authenticator() {
    			protected PasswordAuthentication getPasswordAuthentication() {
    			return new PasswordAuthentication(username, password);
    		try {
    			Message message = new MimeMessage(session);
    			message.setFrom(new InternetAddress(""));
    			message.setSubject("Testing Subject");
    			message.setText("Dear Mail Crawler,"
    				+ "\n\n No spam to my email, please!"
    				+ "Visit");
    		} catch (MessagingException e) {
    			throw new RuntimeException(e);
  4. If you run the as Run as Java Application for first time. You will get the Exception as shown below.
    Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException
    	at email.send_email_gmail.main(
    Caused by: javax.mail.AuthenticationFailedException
    	at javax.mail.Service.connect(
    	at javax.mail.Service.connect(
    	at javax.mail.Service.connect(
    	at javax.mail.Transport.send0(
    	at javax.mail.Transport.send(
    	at email.send_email_gmail.main(

    To solve the exception above you have to enable access to your google account for less secure accounts.
    You can do it here

  5. Now if you Run the the as Run as Java Application. The recipient will get below mail
  6. Send emails from other domains using java SMTP
    Run the below code as Java project. Update the email info with your email details.

    package email;
    import java.util.*; 
    import javax.mail.*;
    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;
    import javax.mail.internet.*;
    public class send_mail {
    	public static void main(String[] args) {
    		send_mail obj_send_mail=new send_mail();
    		obj_send_mail.send_mail("Message Body", "toemailid");
    	public void send_mail(String message,String email_id) {
    		String smtpServer = null; 
                    String smtpPort = null;
    		final String authAddress = "From Email ID";      
    		final String authPassword = "From email id password";    
    		String subject = "Subject Email ID";          
    		//String message = "test by jinu";     
            smtpServer = "SMTP Server";
            smtpPort = "587";    
                    Properties props = new Properties();     
                    props.put("", smtpServer); 
                    props.put("mail.smtp.port", smtpPort);   
                    props.put("mail.smtp.auth", "true");         
                // create some properties and get the default Session
                Session sessionMail = Session.getInstance(props, new Authenticator() {
                     public PasswordAuthentication getPasswordAuthentication() {      
                             return new PasswordAuthentication(authAddress, authPassword);
                // create a message
                Message msg = new MimeMessage(sessionMail);
                // set the from and to address
                InternetAddress addressFrom = new InternetAddress(authAddress);
                InternetAddress[] addressTo = new InternetAddress[1];
                addressTo[0] = new InternetAddress(email_id);
                msg.setRecipients(Message.RecipientType.TO, addressTo);
                // Optional : You can also set your custom headers in the Email if you Want
                msg.addHeader("site", "");
                // Setting the Subject and Content Type
                msg.setContent(message, "text/html");
            }catch(Exception e){


Leave a Comment