Newsletter |
Send Java Email using Spring With Gmail SMTP Server Settings – JavaMailSenderImpl Mail
Spring » on Nov 25, 2012 { 8 Comments } By Sivateja
Let us see how to send java E-mail using Spring Framework, sending E-mail using java is little hassle, we will always get class path or SMTP issues. But Spring providing this powerful class called, org.springframework.mail.javamail.JavaMailSenderImpl having 5 main properties, of which host, port, username, password,javaMailProperties.
Files Required
- ClientLogic.java
- MailLogic.java
- spconfig.xml
Jars Required
- activation.jar
- classes12.jar
- commons-email-1.0.jar
- mail.jar
ClientLogic.java
package java4s; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String[] args) { Resource res = new ClassPathResource("spconfig.xml"); BeanFactory factory = new XmlBeanFactory(res); Object o = factory.getBean("id2"); MailLogic ml = (MailLogic)o; ml.sendM("Enter from email id","Enter to email id","MySubject","This is body"); } }
MailLogic.java
package java4s; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class MailLogic { private MailSender mail; public void setMail(MailSender mail) { this.mail = mail; } public void sendM(String from, String to, String subject, String msg) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(msg); mail.send(message); System.out.println("Mail Sent Successfully...!"); } }
spconfig.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="id1" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="port" value="465" /> <property name="username" value="From Gmail email id" /> <property name="password" value="and it password" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <prop key="mail.smtp.socketFactory.port">465</prop> <prop key="mail.debug">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> </props> </property> </bean> <bean id="id2" class="java4s.MailLogic"> <property name="mail" ref="id1" /> </bean> </beans>
Output
Execution Flow
- ClientLogic.java is our main class, and we are calling id2 [line number 15]
- So come to spconfig.xml and check line number 23, we are calling java4s.MailLogic.java having property mail of type MailSender Interface (provided by spring), so it will moves to JavaMailSenderImpl (id1, implemented class of MailSender) and there we have given all the Email related credentials
- MailSender contains mail() method to send a mail
- MailSender’s mail() method contains SimpleMailMessage object as parameter, like mail(SimpleMailMessage Object)
- Finally we set From,To,Subject,Msg and called send() that’s it
How simple its is ? 🙂
You Might Also Like
::. About the Author .:: | ||
Comments
8 Responses to “Send Java Email using Spring With Gmail SMTP Server Settings – JavaMailSenderImpl Mail”
hello bro..
i want to send a mail from my jsp page to gmail.is it possible by using spring frame work?If it is possible please tell me the solution..it will be helpful to me..
tank u
Very Nice Example….
it is showing an exception as follows.
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name ‘id1’ defined in class path resource [spConfig.xml]: element must have a subelement like ‘value’ or ‘ref’.
plz give the solution
hi,
can you give mail example for spring.
i think you are using oracle, can you give for mysql.
Thank you.
above the program it is not working how is it explain clearly . I faced following error
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 uk5sm11805491pbc.17 – gsmtp
Exception in thread “main” org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException
Caused by: javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:379)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:298)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:284)
at java4s.MailLogic.sendM(MailLogic.java:26)
at java4s.ClientLogic.main(ClientLogic.java:18)
Plz send me the link of all required jars in spring framework
Very helpful tutorial for spring beginers. Easy and understandable language used by author. Thanks
I want to send a mail from my jsp page to gmail.Is it possible by using spring frame work?If it is possible please tell me the solution..it will be helpful to me..
Thank u