{"id":295,"date":"2010-07-29T14:02:00","date_gmt":"2010-07-29T14:02:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/sending-e-mails-in-java-with-spring-gmail-smtp-server-example.html"},"modified":"2012-10-21T19:16:08","modified_gmt":"2012-10-21T19:16:08","slug":"java-mail-spring-gmail-smtp","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html","title":{"rendered":"Sending e-mails in Java with Spring \u2013 GMail SMTP server example"},"content":{"rendered":"<p>For e-mail sending in Java, the <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/index-jsp-139225.html\">JavaMail API<\/a> is the standard solution. As the official web page states, &#8220;The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications&#8221;. The necessary classes are included in the JavaEE platform, but to use it in a standalone JavaSE application you will have to download the corresponding JAR from <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/index-138643.html\">here<\/a>.<\/p>\n<p><span style=\"font-weight: bold\">Note:<\/span> Unless you&#8217;re using Java SE 6, you will also need the JavaBeans Activation Framework (JAF) extension that provides the javax.activation package. We suggest you use version 1.1.1 of JAF, the latest release. JAF is included with Java SE 6.<\/p>\n<p>JavaMail can unfortunately be a little cumbersome and difficult to configure and use. In case you have embraced the <a href=\"http:\/\/www.springsource.org\/\">Spring framework<\/a> for your applications, you will be glad to find out that Spring provides a mail abstraction layer. As the <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.x\/spring-framework-reference\/html\/mail.html\">reference documentation<\/a> states, &#8220;The Spring Framework provides a helpful utility library for sending email that shields the user from the specifics of the underlying mailing system and is responsible for low level resource handling on behalf of the client.&#8221; Great, let&#8217;s see now how to leverage this library.<\/p>\n<p>First, create a new Eclipse project named &#8220;SpringMailProject&#8221;. In the project&#8217;s classpath, make sure to include the following libraries (note that the 3.0.2.RELEASE Spring version was used):<\/p>\n<ul>\n<li>mail.jar (the core JavaMail classes)<\/li>\n<li>org.springframework.asm-3.0.2.RELEASE.jar<\/li>\n<li>org.springframework.beans-3.0.2.RELEASE.jar<\/li>\n<li>org.springframework.context.support-3.0.2.RELEASE.jar<\/li>\n<li>org.springframework.core-3.0.2.RELEASE.jar<\/li>\n<li>org.springframework.expression-3.0.2.RELEASE.jar<\/li>\n<li>com.springsource.org.apache.commons.logging-1.1.1.jar<\/li>\n<\/ul>\n<p><span style=\"font-weight: bold\">Note1:<\/span> The commons-logging library from Apache is needed and was included in the classpath<br \/>\n<span style=\"font-weight: bold\">Note2:<\/span> You will also need the Java Activation Framework if you are using Java 1.5 or earlier<\/p>\n<p>We will use <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.0.RC1\/javadoc-api\/org\/springframework\/mail\/MailSender.html\">MailSender<\/a>, a Spring interface that defines a strategy for sending simple mails. Since this is just an interface, we need a concrete implementation and that comes in the form of <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.0.RC1\/javadoc-api\/org\/springframework\/mail\/javamail\/JavaMailSenderImpl.html\">JavaMailSenderImpl<\/a>. This class supports both JavaMail <a href=\"http:\/\/java.sun.com\/products\/javamail\/javadocs\/javax\/mail\/internet\/MimeMessage.html\">MimeMessage<\/a>s and Spring <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.0.RC1\/javadoc-api\/org\/springframework\/mail\/SimpleMailMessage.html\">SimpleMailMessage<\/a>s.<\/p>\n<p>We will create a simple Spring service that will be used for sending mail. One method will create a <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.0.RC1\/javadoc-api\/org\/springframework\/mail\/SimpleMailMessage.html\">SimpleMailMessage<\/a> on the spot, while the other will use a preconfigured default message. The source code for the service is the following:<\/p>\n<pre class=\"brush: java\">package com.javacodegeeks.spring.mail;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.mail.MailSender;\r\nimport org.springframework.mail.SimpleMailMessage;\r\nimport org.springframework.stereotype.Service;\r\n\r\n@Service(\"mailService\")\r\npublic class MailService {\r\n&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp;&nbsp; @Autowired\r\n&nbsp;&nbsp;&nbsp; private MailSender mailSender;\r\n&nbsp;&nbsp;&nbsp; @Autowired\r\n&nbsp;&nbsp;&nbsp; private SimpleMailMessage alertMailMessage;\r\n&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp;&nbsp; public void sendMail(String from, String to, String subject, String body) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SimpleMailMessage message = new SimpleMailMessage();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; message.setFrom(from);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; message.setTo(to);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; message.setSubject(subject);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; message.setText(body);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mailSender.send(message);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp;&nbsp; }\r\n&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp;&nbsp; public void sendAlertMail(String alert) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mailMessage.setText(alert);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mailSender.send(mailMessage);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp;&nbsp; }\r\n&nbsp;&nbsp; &nbsp;\r\n}<\/pre>\n<p>The class is just a POJO and its service name is &#8220;mailService&#8221;, marked by the stereotype <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.x\/javadoc-api\/org\/springframework\/stereotype\/Service.html\">Service<\/a> annotation. The beans wiring strategy used is the one of <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.x\/spring-framework-reference\/html\/beans.html#beans-factory-autowire\">autowiring<\/a>, thus the <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.x\/javadoc-api\/org\/springframework\/beans\/factory\/annotation\/Autowired.html\">Autowired<\/a> annotation is used. Note that the autowiring can be performed using both the name and the type of a bean.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>The corresponding Spring XML file that bootstraps the container is the following:<\/p>\n<pre class=\"brush: xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n\r\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\r\n&nbsp;&nbsp;&nbsp; xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" \r\n&nbsp;&nbsp;&nbsp; xmlns:p=\"http:\/\/www.springframework.org\/schema\/p\"\r\n&nbsp;&nbsp;&nbsp; xmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\" \r\n&nbsp;&nbsp;&nbsp; xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\r\n&nbsp;&nbsp;&nbsp; xmlns:jee=\"http:\/\/www.springframework.org\/schema\/jee\" \r\n&nbsp;&nbsp;&nbsp; xmlns:task=\"http:\/\/www.springframework.org\/schema\/task\"\r\n&nbsp;&nbsp;&nbsp; xsi:schemaLocation=\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop-3.0.xsd\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http:\/\/www.springframework.org\/schema\/context http:\/\/www.springframework.org\/schema\/context\/spring-context-3.0.xsd\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http:\/\/www.springframework.org\/schema\/jee http:\/\/www.springframework.org\/schema\/jee\/spring-jee-3.0.xsd\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx-3.0.xsd\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http:\/\/www.springframework.org\/schema\/task http:\/\/www.springframework.org\/schema\/task\/spring-task-3.0.xsd\"\r\n&gt;\r\n\r\n&nbsp;&nbsp;&nbsp; &lt;context:component-scan base-package=\"com.javacodegeeks.spring.mail\" \/&gt;&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; &lt;bean id=\"mailSender\" class=\"org.springframework.mail.javamail.JavaMailSenderImpl\"&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=\"host\" value=\"smtp.gmail.com\"\/&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=\"port\" value=\"25\"\/&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=\"username\" value=\"myusername@gmail.com\"\/&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=\"password\" value=\"mypassword\"\/&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=\"javaMailProperties\"&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;props&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- Use SMTP transport protocol --&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;prop key=\"mail.transport.protocol\"&gt;smtp&lt;\/prop&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- Use SMTP-AUTH to authenticate to SMTP server --&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;prop key=\"mail.smtp.auth\"&gt;true&lt;\/prop&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- Use TLS to encrypt communication with SMTP server --&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;prop key=\"mail.smtp.starttls.enable\"&gt;true&lt;\/prop&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;prop key=\"mail.debug\"&gt;true&lt;\/prop&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/props&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/property&gt;\r\n&nbsp;&nbsp;&nbsp; &lt;\/bean&gt;\r\n&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; &lt;bean id=\"alertMailMessage\" class=\"org.springframework.mail.SimpleMailMessage\"&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=\"from\"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;value&gt;myusername@gmail.com&lt;\/value&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/property&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=\"to\"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;value&gt;myusername@gmail.com&lt;\/value&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/property&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=\"subject\" value=\"Alert - Exception occurred. Please investigate\"\/&gt;\r\n&nbsp;&nbsp;&nbsp; &lt;\/bean&gt;\r\n&nbsp;&nbsp;&nbsp; \r\n&lt;\/beans&gt;<\/pre>\n<p>This is a quite simple Spring config file. Some things to mention though:<\/p>\n<ul>\n<li>The base package from which the context scanning will start is declared and is set to &#8220;com.javacodegeeks.spring.mail&#8221;.<\/li>\n<li>The &#8220;mailSender&#8221; bean is declared and a bunch of its properties are appropriately set (use your own  SMTP server configuration attributes and credentials).<\/li>\n<li>The &#8220;alertMailMessage&#8221; is a preconfigured Spring SimpleMailMessage and this will be injected in the &#8220;MailService&#8221; class using &#8220;by name autowiring&#8221;.<\/li>\n<\/ul>\n<p>In case you wish to use Gmail&#8217;s SMTP server, make sure that the following JavaMail properties are configured appropriately:<\/p>\n<pre class=\"brush: bash\">host=smtp.gmail.com\r\nport=25\r\nusername=your-gmail-username\r\npassword=your-gmail-password\r\nmail.transport.protocol=smtp\r\nmail.smtp.auth=true\r\nmail.smtp.starttls.enable=true<\/pre>\n<p>While in development phase, set &#8220;mail.debug&#8221; to true if you want the mail client to generate informative logs. However, remember to set this to false when in production, because the amount of logs could degrade the application&#8217;s performance and\/or fill-up your hard disks.<\/p>\n<p>Finally, we create a class that will be used to create the Spring container and test the simple mail service we created. The source code is the following:<\/p>\n<pre class=\"brush: java\">package com.javacodegeeks.spring.mail;\r\n\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.support.FileSystemXmlApplicationContext;\r\n\r\npublic class MailServiceTest {\r\n\r\n&nbsp;&nbsp;&nbsp; public static void main(String[] args) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ApplicationContext context = new FileSystemXmlApplicationContext(\"conf\/spring.xml\");\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MailService mailService = (MailService) context.getBean(\"mailService\");\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mailService.sendMail(\"myusername@gmail.com\", \"myusername@gmail.com\", \"Testing123\", \"Testing only \\n\\n Hello Spring Email Sender\");\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mailService.sendAlertMail(\"Exception occurred\");\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \r\n&nbsp;&nbsp;&nbsp; }\r\n&nbsp;&nbsp;&nbsp; \r\n}<\/pre>\n<p>The <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.0.RC1\/javadoc-api\/org\/springframework\/context\/support\/FileSystemXmlApplicationContext.html\">FileSystemXmlApplicationContext<\/a> class is used as the <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.0.RC1\/javadoc-api\/org\/springframework\/context\/ApplicationContext.html\">ApplicationContext<\/a>. We pass the Spring&#8217;s XML file location in the constructor and the class creates the container, instantiates the beans and takes care of the dependencies. Don&#8217;t you love Spring? Next, we retrieve a reference of our &#8220;MailService&#8221; service using the &#8220;<a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.0.RC1\/javadoc-api\/org\/springframework\/context\/support\/AbstractApplicationContext.html#getBean%28java.lang.String%29\">getBean<\/a>&#8221; method and invoke the two methods.<\/p>\n<p>That&#8217;s all. As always, you can download the full Eclipse project from <a href=\"http:\/\/dl.dropbox.com\/u\/7215751\/JavaCodeGeeks\/SpringMailTutorial\/SpringMailProject.zip\">here<\/a>.<\/p>\n<div style=\"margin-bottom: 0px;margin-left: 0px;margin-right: 0px;margin-top: 0px\"><strong><i>Related Articles :<\/i><\/strong><\/div>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/12\/dependency-injection-manual-way.html\">Dependency Injection &#8211; The manual way<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/12\/securing-gwt-apps-with-spring-security.html\">Securing GWT apps with Spring Security<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/11\/jaxws-with-spring-and-maven-tutorial.html\">JAX\u2013WS with Spring and Maven Tutorial<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/07\/building-your-own-gwt-spring-manen.html\">Building your own GWT Spring Maven Archetype<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/07\/aspect-oriented-programming-with-spring.html\">Aspect Oriented Programming with Spring AspectJ and Maven<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/06\/spring-3-restful-web-services.html\">Spring 3 RESTful Web Services<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/05\/jboss-42x-spring-3-jpa-hibernate.html\">JBoss 4.2.x Spring 3 JPA Hibernate Tutorial<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>For e-mail sending in Java, the JavaMail API is the standard solution. As the official web page states, &#8220;The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications&#8221;. The necessary classes are included in the JavaEE platform, but to use it in a standalone JavaSE application you will have to &hellip;<\/p>\n","protected":false},"author":3,"featured_media":110,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[29,64,63,30],"class_list":["post-295","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-eclipse","tag-mail","tag-smtp","tag-spring"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sending e-mails in Java with Spring \u2013 GMail SMTP server example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"For e-mail sending in Java, the JavaMail API is the standard solution. As the official web page states, &quot;The JavaMail API provides a platform-independent\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sending e-mails in Java with Spring \u2013 GMail SMTP server example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"For e-mail sending in Java, the JavaMail API is the standard solution. As the official web page states, &quot;The JavaMail API provides a platform-independent\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2010-07-29T14:02:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T19:16:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/eclipse-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ilias Tsagklis\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ilias Tsagklis\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html\"},\"author\":{\"name\":\"Ilias Tsagklis\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9a83496b285d30c61e8a674625c1350e\"},\"headline\":\"Sending e-mails in Java with Spring \u2013 GMail SMTP server example\",\"datePublished\":\"2010-07-29T14:02:00+00:00\",\"dateModified\":\"2012-10-21T19:16:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html\"},\"wordCount\":730,\"commentCount\":12,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/eclipse-logo.jpg\",\"keywords\":[\"Eclipse\",\"Mail\",\"SMTP\",\"Spring\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html\",\"name\":\"Sending e-mails in Java with Spring \u2013 GMail SMTP server example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/eclipse-logo.jpg\",\"datePublished\":\"2010-07-29T14:02:00+00:00\",\"dateModified\":\"2012-10-21T19:16:08+00:00\",\"description\":\"For e-mail sending in Java, the JavaMail API is the standard solution. As the official web page states, \\\"The JavaMail API provides a platform-independent\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/eclipse-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/eclipse-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/07\\\/java-mail-spring-gmail-smtp.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Sending e-mails in Java with Spring \u2013 GMail SMTP server example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9a83496b285d30c61e8a674625c1350e\",\"name\":\"Ilias Tsagklis\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g\",\"caption\":\"Ilias Tsagklis\"},\"description\":\"Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.\",\"sameAs\":[\"http:\\\/\\\/www.iliastsagklis.com\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/iliastsagklis\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/ilias-tsagklis\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sending e-mails in Java with Spring \u2013 GMail SMTP server example - Java Code Geeks","description":"For e-mail sending in Java, the JavaMail API is the standard solution. As the official web page states, \"The JavaMail API provides a platform-independent","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html","og_locale":"en_US","og_type":"article","og_title":"Sending e-mails in Java with Spring \u2013 GMail SMTP server example - Java Code Geeks","og_description":"For e-mail sending in Java, the JavaMail API is the standard solution. As the official web page states, \"The JavaMail API provides a platform-independent","og_url":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2010-07-29T14:02:00+00:00","article_modified_time":"2012-10-21T19:16:08+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/eclipse-logo.jpg","type":"image\/jpeg"}],"author":"Ilias Tsagklis","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Ilias Tsagklis","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html"},"author":{"name":"Ilias Tsagklis","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9a83496b285d30c61e8a674625c1350e"},"headline":"Sending e-mails in Java with Spring \u2013 GMail SMTP server example","datePublished":"2010-07-29T14:02:00+00:00","dateModified":"2012-10-21T19:16:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html"},"wordCount":730,"commentCount":12,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/eclipse-logo.jpg","keywords":["Eclipse","Mail","SMTP","Spring"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html","url":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html","name":"Sending e-mails in Java with Spring \u2013 GMail SMTP server example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/eclipse-logo.jpg","datePublished":"2010-07-29T14:02:00+00:00","dateModified":"2012-10-21T19:16:08+00:00","description":"For e-mail sending in Java, the JavaMail API is the standard solution. As the official web page states, \"The JavaMail API provides a platform-independent","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/eclipse-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/eclipse-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2010\/07\/java-mail-spring-gmail-smtp.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Sending e-mails in Java with Spring \u2013 GMail SMTP server example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9a83496b285d30c61e8a674625c1350e","name":"Ilias Tsagklis","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/43505f28bb49f6e290c24be0b209ccc1af350f0f6587025ffd4847ef44bf6b78?s=96&d=mm&r=g","caption":"Ilias Tsagklis"},"description":"Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.","sameAs":["http:\/\/www.iliastsagklis.com\/","https:\/\/www.linkedin.com\/in\/iliastsagklis"],"url":"https:\/\/www.javacodegeeks.com\/author\/ilias-tsagklis"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/110"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}