This is my code for sending mail in HTML format:

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
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 SendMail {
    public void sendMail(String m_from,String m_to,String m_subject,String m_body){
      try {
          Session m_Session;
          Message m_simpleMessage;
          InternetAddress m_fromAddress;
          InternetAddress m_toAddress;
          Properties m_properties;

          m_properties     = new Properties();
          m_properties.put("mail.smtp.host", "smtp.gmail.com"); 
          m_properties.put("mail.smtp.socketFactory.port", "465");
          m_properties.put("mail.smtp.auth", "true");
          m_properties.put("mail.smtp.port", "465");

          m_Session=Session.getDefaultInstance(m_properties,new Authenticator() {
               protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("xxxxx","yyyyy"); // username and the password

          m_simpleMessage  =   new MimeMessage(m_Session);
          m_fromAddress    =   new InternetAddress(m_from);
          m_toAddress      =   new InternetAddress(m_to);
          m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);

          m_simpleMessage.setContent(m_body, "text/html");       

        } catch (MessagingException ex) {

  public static void main(String[] args) {
      SendMail send_mail    =   new SendMail();
      String empName = "Antony Raj S";
      String title ="<b>Hi !"+empName+"</b>";
      send_mail.sendMail("anto@gmail.com", "anthony@slingmedia.com", "Please apply for leave for the following dates", title+"<br>by<br><b>HR<b>");

This works fine, but is there any other better way to do the same or how can I make the same code still better one? I get all the values like from address, SMTP host, port from the contex.xml file which is there in conf folder of Tomcat, so please do suggest anything better.

  • \$\begingroup\$ here is a similar question: stackoverflow.com/questions/322298/… \$\endgroup\$
    – tibtof
    Jun 13 '12 at 11:46
  • \$\begingroup\$ if you change the host to smtp.bizmail.yahoo.com it will be better like the mail will be sent to any mail like ss@tcs.com \$\endgroup\$
    – pardhu
    Jun 13 '12 at 11:47
  • \$\begingroup\$ This JavaMail FAQ entry will help you improve your code. \$\endgroup\$
    – user14122
    Jun 13 '12 at 19:41
  1. Try to minimize the scope of local variables. It's not necessary to declare them at the beginning of the method, declare them where they are first used.

    See Effective Java, Second Edition, Item 45: Minimize the scope of local variables. (Google for "minimize the scope of local variables", it's on Google Books too.))

  2. The m_ prefix is unnecessary and uncommon in the Java world. See Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions

  3. The sendMail method should validate its input parameters. Does it make sense if the form, to etc. is null or empty string? If not, check it and throw a NullPointerException or an IllegalArgumentException. (Effective Java, Second Edition, Item 38: Check parameters for validity)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy