Java Email

This java tutorial is to send email in java using Gmail. Example java email program will use Gmail SMTP server to send mail.
There is not much involved to discuss as javamail api takes care of everything. It is all about just using the JavaMail api. By every release, javamail api is getting sophisticated and so it is an easy job.
I have used JavaMail API version 1.4.5 and should add two jars as dependency for sending email mailapi.jar and smtp.jar

  • My example program uses Gmail SMTP server.
  • Mail authentication is set to true and need to give sender’s email and password. That is, we cannot send email anonymously.
  • In the program you can modify it to send to multiple To emails.
  • Sample uses HTML email as content type.
  • You can add email attachment to emailMessage.
  • To send email and test the program, just change fromUser and fromUserEmailPassword. Yes it is as simple as that.
package com.java;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaEmail {
  Properties emailProperties;
  Session mailSession;
  MimeMessage emailMessage;
  public static void main(String args[]) throws AddressException,
      MessagingException {
    JavaEmail javaEmail = new JavaEmail();
    javaEmail.setMailServerProperties();
    javaEmail.createEmailMessage();
    javaEmail.sendEmail();
  }
  public void setMailServerProperties() {
    String emailPort = "587";//gmail's smtp port
    emailProperties = System.getProperties();
    emailProperties.put("mail.smtp.port", emailPort);
    emailProperties.put("mail.smtp.auth", "true");
    emailProperties.put("mail.smtp.starttls.enable", "true");
  }
  public void createEmailMessage() throws AddressException,
      MessagingException {
    String[] toEmails = { "hareesh.nare@gmail.com" };
    String emailSubject = "Java Email";
    String emailBody = "This is an email sent by <b>JavaMail</b> api.";
    mailSession = Session.getDefaultInstance(emailProperties, null);
    emailMessage = new MimeMessage(mailSession);
    for (int i = 0; i < toEmails.length; i++) {
      emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
    }
    emailMessage.setSubject(emailSubject);
    emailMessage.setContent(emailBody, "text/html");//for a html email
    //emailMessage.setText(emailBody);// for a text email
  }
  public void sendEmail() throws AddressException, MessagingException {
    String emailHost = "smtp.gmail.com";
    String fromUser = "your emailid here";//just the id alone without @gmail.com
    String fromUserEmailPassword = "your email password here";
    Transport transport = mailSession.getTransport("smtp");
    transport.connect(emailHost, fromUser, fromUserEmailPassword);
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
    transport.close();
    System.out.println("Email sent successfully.");
  }
}

0 comments:

Post a Comment