Monday, 23 November 2015

Sending Email using Spring Framework


Step-1

Add dependency

<!-- Spring API for mailing support org.springframework.mail.javamail.JavaMailSenderImpl --> 
        <dependency> 
               <groupId>org.springframework</groupId> 
                <artifactId>spring-context-support</artifactId>
               <version>${spring.version}</version> 
            </dependency>

<!-- Java Mail API -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>

</dependency>


Step-2
create spring meta configuration file..
spring-email-service.xml

  <bean id="synergyMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" lazy-init="true">
        <property name="host" value="smtp.gmail.com"/>
        <property name="port" value="587"/>
        <property name="username" value="coolcool@gmail.com"/>
        <property name="password" value="desibank"/>
        <property name="javaMailProperties">
            <props>
                <!-- Use SMTP transport protocol -->
                <prop key="mail.transport.protocol">smtp</prop>
                <!-- Use SMTP-AUTH to authenticate to SMTP server -->
                <prop key="mail.smtp.auth">true</prop>
                <!-- Use TLS to encrypt communication with SMTP server -->
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.debug">false</prop>
            </props>
        </property>

    </bean>


Step-3

Define the interface

/**
 * 
 * @author nagendra.yadav
 *
 */
public interface BankEmailService {

public void sendMail(String from,String to,String subject,String body);
public void sendMail(String from, String[] to, String subject, String body);
public void sendBirthdayEmails(String from, String to, String subject,
String body, String name);


}


Step-4
Write Service Implementation

import org.springframework.mail.javamail.JavaMailSender;

@Service("BankEmailServiceImpl")
public class BankEmailServiceImpl implements BankEmailService {

@Autowired
@Qualifier("synergyMailSender")
private JavaMailSender mailSender;

@Override
public void sendMail(String from, String to, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();
       message.setFrom(from);
       message.setTo(to);
       message.setSubject(subject);
       message.setText(body);
       mailSender.send(message);
}

@Override
public void sendMail(String from, String[] to, String subject, String body) {
   SimpleMailMessage message = new SimpleMailMessage();
           message.setFrom(from);
           message.setTo(to);
           message.setSubject(subject);
           message.setText(body);
           mailSender.send(message);

}

}


Step-5
Sending email using thread to make it asynchronous 

public class EmailSenderThread extends Thread {

private  BankEmailService bankEmailService;
private String toEMail;
private String message;
private String subject;

public EmailSenderThread(BankEmailService bankEmailService, String toEMail,
String message,String subject) {
this.bankEmailService = bankEmailService;
this.toEMail = toEMail;
this.message = message;
this.subject=subject;
}

public EmailSenderThread(String tname){
 super(tname);
}

public void run() {
try {
bankEmailService.sendMail("cool@gmail.com",toEMail,subject,message);
}catch (Exception e) {
e.printStackTrace();
}
}

}


Step-6

Send the email using below code

EmailSenderThread emailSenderThread = new EmailSenderThread(
bankEmailService,
customerAccountForm.getCustomerEmail(), body,
"Account creation notification !");

emailSenderThread.start();



No comments:

Post a Comment