Sample Mail Sending Implementation Using spring 3.0
hello friends i am going to present you a simple example of how to send mail using spring.however you can also use java mail api to send mails.but spring is also providing inbuilt classes and interfaces through which you can send mail.
i am going to show you simple example of sending mail using gmail server .here it is:
package org.demo.mail;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
@Service("mailService")
public class MailSendingService
{
@Autowired
private MailSender mailSender;
private SimpleMailMessage preConfiguredMessage;
public void sendMail(String to,String subject,String body)
{
SimpleMailMessage message=new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
public void sendPreConfiguredMail(String message)
{
SimpleMailMessage sm=new SimpleMailMessage(preConfiguredMessage);
sm.setText(message);
mailSender.send(sm);
}
}
Client class:
package org.demo.mail;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MailClient
{
public static void main(String args[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("Spring-Config.xml");
MailSendingService mailer=(MailSendingService)context.getBean("mailService");
mailer.sendMail("*******@gmail.com", "hi", "Done");
mailer.sendPreConfiguredMail("Exception,try again ...!!! ");
;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.demo.mail">
</context:component-scan>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"/>
<property name="port" value="25"/>
<property name="username" value="*********@gmail.com"/>
<property name="password" value="**********"/>
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="preConfiguredMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="to" value="********@gmail.com"></property>
<property name="from" value="*******@gmail.com"></property>
<property name="subject" value="Fatal"></property>
</bean>
</beans>
thanks
for any query ping me @ pathak.nisarg@yahoo.com
hello friends i am going to present you a simple example of how to send mail using spring.however you can also use java mail api to send mails.but spring is also providing inbuilt classes and interfaces through which you can send mail.
i am going to show you simple example of sending mail using gmail server .here it is:
package org.demo.mail;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
@Service("mailService")
public class MailSendingService
{
@Autowired
private MailSender mailSender;
private SimpleMailMessage preConfiguredMessage;
public void sendMail(String to,String subject,String body)
{
SimpleMailMessage message=new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
public void sendPreConfiguredMail(String message)
{
SimpleMailMessage sm=new SimpleMailMessage(preConfiguredMessage);
sm.setText(message);
mailSender.send(sm);
}
}
Client class:
package org.demo.mail;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MailClient
{
public static void main(String args[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("Spring-Config.xml");
MailSendingService mailer=(MailSendingService)context.getBean("mailService");
mailer.sendMail("*******@gmail.com", "hi", "Done");
mailer.sendPreConfiguredMail("Exception,try again ...!!! ");
;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.demo.mail">
</context:component-scan>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"/>
<property name="port" value="25"/>
<property name="username" value="*********@gmail.com"/>
<property name="password" value="**********"/>
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="preConfiguredMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="to" value="********@gmail.com"></property>
<property name="from" value="*******@gmail.com"></property>
<property name="subject" value="Fatal"></property>
</bean>
</beans>
thanks
for any query ping me @ pathak.nisarg@yahoo.com