Spring – Sending E-mail via Gmail SMTP server with MailSender
Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify the e-mail sending process via JavaMail API. Here’s a Maven build project to use Spring’s ‘JavaMailSenderImpl‘ to send an email via Gmail SMTP server.
1. Project dependency
Add the JavaMail and Spring’s dependency.
File : pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.common</groupId> <artifactId>SpringExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SpringExample</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>Java.Net</id> <url>http://download.java.net/maven/2/</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Java Mail API --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.3</version> </dependency> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> </dependencies> </project>
2. Spring’s Mail Sender
A Java class to send email with the Spring’s MailSender interface.
File : MailMail.java
package com.mkyong.common; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class MailMail { private MailSender mailSender; public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public void sendMail(String from, String to, String subject, String msg) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(msg); mailSender.send(message); } }
3. Bean configuration file
Configure the mailSender bean and specify the email details for the Gmail SMTP server.
Note
Gmail configuration details – http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
Gmail configuration details – http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
File : Spring-Mail.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="port" value="587" /> <property name="username" value="username" /> <property name="password" value="password" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> </props> </property> </bean> <bean id="mailMail" class="com.mkyong.common.MailMail"> <property name="mailSender" ref="mailSender" /> </bean> </beans>
4. Run it
package com.mkyong.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml"); MailMail mm = (MailMail) context.getBean("mailMail"); mm.sendMail("from@no-spam.com", "to@no-spam.com", "Testing123", "Testing only \n\n Hello Spring Email Sender"); } }
Download Source Code
Download it – Spring-Email-Gmail-Smtp-Example.zip

how to send emails using gwt and spring and hibernate.
how to send emails using gwt and spring.
Hi,
Thanks for this tutorial. This is really effective.
One issue I am facing while running this code. The mail is getting delivered without the subject and from.
Can you please let me know apart from this code do I need to do anything else.
My Code:
package com.cms.notification.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class SampleMail {
@Autowired
private MailSender mailSender;
public void sendMail(String from, String to,String subject, String msg){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(msg);
mailSender.send(message);
System.out.println(“DONE”);
}
}
Regards,
Shruti
Hi. Error in Spring Web MVC. java.lang.NullPointerException.
I have downloaded your source code but I see your code in this article is quite different from your code in source code. Which code should I refer?
May I know what are the differences? Just try run it yourself then you will know which code should follow :)
Try this tutorial..see if you like it better http://www.cavalr.com/blog/Send_Email_with_Spring_using_Velocity_template
Thank you dude!
nice tutorial boss
Hi I’m using javamailproperties in the mail bean. I want bounced email notification to be send to another address rather than “from” address in the mail. I’m not getting error but bounced mails are not being sent either.
bean properties
java mail sender is
can you help me find what is the problem please? I’ve been working on this for 2 days now :(
Thank you
I resolved my issue.
http://www.coderanch.com/t/594332/Spring/spring-javamailproperties-mail-smtp-not#2711259
Nice work mkong!!! Keep it coming
Hi,
i just followed your tutorial and got a “org.springframework.mail.MailAuthenticationException” but managed to solve it. Problem was that freshly created gmail -account is blocked somehow, tried with my own personal account and problem was gone.
code works fine but mail is not recieved at the other hand. please help..
I getting the error as below when I am trying the send the email via the spring mvc framework. I could not figure out the problem. Can anyone help me with the solution?
Is the below error meant that I cant access the smtp of the gmail host?
DEBUG: JavaMail version 1.4.3
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host “smtp.gmail.com”, port 25, isSSL false
Is there any way to know whether the email has been successfully delivered or not. I meant to say whether any bounce back message is there?
Thanks a lot, it was really useful. There is a
in the project that can be downloaded which isn’t really used (and it’s not used in the example on this page), you may want to delete in there.
Do you know how to get a delivery notificaiton of an email sent by using JavaMailSenderImpl.
I have tried to set the following properties and tried, but it doesn’t work.
true
true
true
ISO-8859-1
“SUCCESS, FAILURE ORCPT=rfc822;myemail@anyhost.com”
HDRS
Great Tutorial !!! Thanks Mkyoung !
is it work for port=465 for secure connection
Why they call localhost with port 25 ??
Make sure you set this correctly, Gmail post is 587
I have done above example but its giving runtime exeception as:
Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. xu6sm19670673igb.7
Thanks for you example.
BTW, does anyone know can we use this for commercial purpose? Does it violate any Gmail’s rules?
Contact Gmail team for more information.
Nice tutorial,
i want to insert hyperlink in mail, how to do it with spring please tell me any one knows….
cool!
I’m getting following error please help :(
Process finished with exit code 1
Thanks.
Are you behind Firewall or proxy? Make sure your network didn’t blocked smtp.gmail.com and port 587. Often time, your company just blocked anything but port 80.
Yes you are right after turn off the firewall sample code worked.
Thanks.
good?very gook?I use the Macfee and it intercept?so it is always error?
Nice tutorial…. it works…
How can i set proxy settings here, to send emails through network that uses a proxy server?
Thanks!
Hi,
Nice tutorail, but
org.springframework.mail.MailSender
is not resolved
Where I can get jar…?
MailSender is belong to Spring core, see Maven dependencies below
Nice tuto guy!!!
Tks a lot ;-)