Send e-mail using Java mail

Send Mail
In this blog, we are going to learn how to send e-mail using java mail API.

A Brief Idea

Java Mail API provides a platform-independent and protocol-independent framework to build mail and messaging applications.The JavaMail API provides facilities for reading and sending email. Service providers implement particular protocols. Several service providers are included with the JavaMail API package; others are available separately. The JavaMail API is implemented as a Java optional package that can be used on JDK 1.4 and later on any operating system.

For more information about , Click here

You must use your own mail server, or one provided by your Internet Service Provider or the company you work for. Your network administrator can give you the information necessary to configure JavaMail to work with your mail server.

I am going to write a general purpose java class that can send mail from any valid email account. User can send mail using his/her valid Gmail/YahooMail/Others email account.

Configuration parameters for Gmail Account 
  • Host Server Name/IP : smtp.gmail.com
  • Port : 465
Configuration parameters for YahooMail Account
  • Host Server Name/IP : smtp.mail.yahoo.com
  • Port : 465
 Configuration parameters for personal mail server
  • Host Server Name/IP : IP address of mail your server/name
  • Port : 25 [default]


Here We Go
The java file name is  SendMailUsingJavaMail.java
 
package simple.javamail;
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;

public class SendMailUsingJavaMail
{
  private String USER_ID = null;
  private String USER_PWD = null;
  
  public String sendEmail(String strHostName,String nPort,
   String strSenderEmailAddress,String strSenderEmailPassword,
   String strRecipientEmailAddress,String strMessageContent,
   String strSubject,boolean isSSL)
  {
    USER_ID = strSenderEmailAddress;
    USER_PWD = strSenderEmailPassword;
    
    Session session = null;
    MimeMessage mimeMsg = null;
    
    //Create properties, get Session
    Properties props = new Properties();
    //The SMTP server to connect to
    props.put("mail.smtp.host", strHostName);
    //The SMTP server port to connect to
    props.put("mail.smtp.port",nPort);
    
    //Default user name for SMTP. Sender email account
    props.put("mail.smtp.user",strSenderEmailAddress);
    props.put("mail.smtp.auth", "true");
     
    //Used for SSL
    if(isSSL)
    {
     props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
     props.put("mail.smtp.socketFactory.fallback", "false");
    }
    
    // To see what is going on behind the scene
    props.put("mail.debug", "true");
    try
    {
     SMTPAuthenticator auth = (SMTPAuthenticator) new SMTPAuthenticator();
     session = Session.getInstance(props, auth);
     session.setDebug(true);
     mimeMsg = new MimeMessage(session);
     mimeMsg.setText(strMessageContent);
     mimeMsg.setSubject(strSubject);
     mimeMsg.setFrom(new InternetAddress(strSenderEmailAddress));
     mimeMsg.addRecipient(Message.RecipientType.TO,new 
     InternetAddress(strRecipientEmailAddress));
     Transport.send(mimeMsg);
     return "SUCCEESS";
    }
    catch (MessagingException mEx)
    {
     mEx.getMessage();
     return "FAILED";
    }
    finally
    {
     // Write your cleaning stuff here
    }
  }
   
  /* Inner class used for user authentication */
  class SMTPAuthenticator extends javax.mail.Authenticator
  {
   public PasswordAuthentication getPasswordAuthentication()
   {
    return new PasswordAuthentication(USER_ID,USER_PWD);
   }
  }
}



Now, You can use this class to send an email from any valid email account.You have to pass the appropriate parameters to the method sendEmail(...).




Here is the description of parameters of the method sendEmail(...).

  • StrHostName : The host name of the mail server for the specified protocol. For e.g. ,smtp.mail.yahoo.com / smtp.gmail.com / yourserver.domain.com,IP
  • nPort : The port number of the mail server for the specified protocol. If not specified the protocol's default port number is used. For eg., 25 / 465
  • strSenderEmailPassword : Password of sender email account. For e.g., passwordABC
  • strMessageContent : Content of the mail. It can be in simple text format or in html  tag format. In case of html format , you have to use setContent() method as mimeMsg.setContent(strMessageContent,"text/html");


Comments are closed.