A simple program to send email in JAVA

We will see a very simple program to send the email in JAVA using JAVAMail API. Please download all the jar files and include in the library.

Code to send email :


import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;

public class SendMailUsingAuthentication
{

  private static final String SMTP_HOST_NAME = "SMTP_HOST_NAME";
  private static final String SMTP_PORT = "465";

  private static final String SMTP_AUTH_USER = "SENDER_EMAIL";
  private static final String SMTP_AUTH_PWD  = "SENDER_PASS";

  private static final String emailMsgTxt      = "-----------Hello------------------ ";
  private static final String emailSubjectTxt  = "Test Mail from JAVA";
	private static final String emailFromAddress = "FROM_EMAIL";

  // Add List of Email address to who email needs to be sent to
	private static final String[] emailList = { "TO_EMAIL_USER1","TO_EMAIL_USER2" };

  public static void main(String args[]) throws Exception
  {
    SendMailUsingAuthentication mailSender = new SendMailUsingAuthentication();
	mailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
    System.out.println("Sucessfully Sent mail to All Users");
  }

  public void postMail( String recipients[ ], String subject,
                            String message , String from) throws MessagingException
  {
    boolean debug = false;
            //Set the host smtp address
	Properties props = new Properties();
	props.put("mail.smtp.host", SMTP_HOST_NAME);
	props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            //IF SSL required by Host
	props.put("mail.smtp.socketFactory.class" , "javax.net.ssl.SSLSocketFactory");
	props.put("mail.smtp.auth", "true");
	props.put("mail.debug", "true");
	props.put("mail.smtp.port", SMTP_PORT);

            //Authenticate the Username and password for the SMTP
	Authenticator auth = new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);

    Session session = Session.getDefaultInstance(props, auth);

    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    System.out.println("Sending Email...........");
    Transport.send(msg);

    System.out.println("Email Sent.........");

 }

}

Posted

in

by

Tags:


Related Posts

Comments

2 responses to “A simple program to send email in JAVA”

  1. dev Avatar
    dev

    IT’s not working.

    Got this error

    found : javax.mail.PasswordAuthentication
    required: javax.mail.Authenticator
    Authenticator auth = new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
    1 error

    1. JitendraZaa Avatar
      JitendraZaa

      You have ti import “javax.mail.Authenticator”, instead of “javax.mail.PasswordAuthentication”

Leave a Reply to devCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading