Showing posts with label Email. Show all posts
Showing posts with label Email. Show all posts

Spring - Sending E-Mail with MailSender

Sending email using Spring "org.springframework.mail.javamail.JavaMailSenderImpl" class simplifies the process with JavaMail API. Below example will give step by step to send email by using Gmail SMTP server configurations.


<project xmlns="" xmlns:xsi=""


  <!-- Spring framework -->

  <!-- Java Mail API -->



<beans xmlns=""

 <bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host" value="" />
  <property name="port" value="587" />
  <property name="username" value="USERNAME" />
  <property name="password" value="PASSWORD" />

  <property name="javaMailProperties">
    <prop key="mail.smtp.auth">true</prop>
    <prop key="mail.smtp.starttls.enable">true</prop>

 <bean id="emailBean" class="">
  <property name="emailSender" ref="emailSender" />



import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class Mailer {

 private MailSender emailSender;
 //Setter for emailSender
 public void setEmailSender(MailSender emailSender) {
  this.emailSender = emailSender;
 //Method to send email
 public void sendMail(String from, String to, String subject, String msg) {

  SimpleMailMessage simpleMsg = new SimpleMailMessage();
  //Finally sending email using MailSender


import org.springframework.context.ApplicationContext;

public class SendEmail {

 public static void main(String[] args) {

  String from = "";
  String to = "";
  String subject = "Hello World !";
  String msg = "Hello World, How are you ???";

  ApplicationContext context = new ClassPathXmlApplicationContext("email.xml");

  try {
   Mailer mailer = (Mailer) context.getBean("emailBean");
   mailer.sendMail(from, to, subject, msg);

  } catch (Exception e) {

Spring - Sending E-Mail with MailSender

Sending email using Java Mail API

In this tutorial we will see how to send email using Java Mail API. We are using Gmail SMTP host for sending email in the below sample code.
Mainly we need 2 jar files to implement Java Mail API. Those jar's are

  • activation-1.0.2.jar
  • mail-1.4.1.jar

Below code have tested along with above jar files. 

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 SendEmailViaGmail {
 public static void main(String[] args) {
  // To address
  String to = "";
  // If any CC email ids
  String cc = "";
  // If any BCC email ids
  String bcc = "";
  // Email Subject
  String subject = "Java Discover";
  // Email content
  String emailText = "Hi All, Welcome to Java Discover";

  // Sending Email using Gmail SMTP
  sendEmail(to, cc, bcc, subject, emailText);

 public static void sendEmail(String to, String cc, String bcc, String subject, String emailText) {
  // From address (Need Gmail ID)
  String from = "";
  // Password of from address
  String password = "frompassword";
  // Gmail host address
  String host = "";
  Properties props = System.getProperties();
  props.put("", host);
  props.put("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.socketFactory.class", "");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", "465");
  props.put("mail.smtp.user", from);
  props.put("password", password);

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

  MimeMessage msg = new MimeMessage(session);
  try {
   msg.setFrom(new InternetAddress(from));
   // Adding To address
   msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
   // Adding CC email id
   msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
   // Adding BCC email id
   msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));

   Transport transport = session.getTransport("smtp");
   transport.connect(host, from, password);
   transport.sendMessage(msg, msg.getAllRecipients());
   System.out.println("Email sent successfully.....");
  } catch (AddressException e) {
  } catch (MessagingException e) {

Suppose if you are getting exception while running above like, PKIX path building failed: unable to find valid certification path to requested target

then valid SSL certificate is missing in the machine which your running. Follow the steps given in link and try again.