Get all updates of Java4s
Follow Us: Email Newsletter

Send Java Email using Spring With Gmail SMTP Server Settings – JavaMailSenderImpl Mail

Spring » On Nov 25, 2012 { 6 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

MailLogic.java

spconfig.xml

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 Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

6 Responses to “Send Java Email using Spring With Gmail SMTP Server Settings – JavaMailSenderImpl Mail”
  1. Manoj says:

    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

  2. Jagannath says:

    Very Nice Example….

  3. sandeep says:

    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

  4. sri says:

    hi,
    can you give mail example for spring.
    i think you are using oracle, can you give for mysql.
    Thank you.

  5. palanikumar says:

    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)

  6. Mohd Islam says:

    Plz send me the link of all required jars in spring framework

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.