0% found this document useful (0 votes)
158 views

Spring Email Support

This document provides instructions for configuring Spring to send emails using JavaMail and Gmail SMTP server. It describes adding dependencies, creating a MailSender bean, sending emails with a template, and attaching files. The steps are: 1. Add JavaMail and Spring dependencies 2. Create a MailSender bean configured for Gmail SMTP 3. Define a mail template and MailSender bean 4. Send emails using the MailSender, optionally with attachments

Uploaded by

govindbirajdar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

Spring Email Support

This document provides instructions for configuring Spring to send emails using JavaMail and Gmail SMTP server. It describes adding dependencies, creating a MailSender bean, sending emails with a template, and attaching files. The steps are: 1. Add JavaMail and Spring dependencies 2. Create a MailSender bean configured for Gmail SMTP 3. Define a mail template and MailSender bean 4. Send emails using the MailSender, optionally with attachments

Uploaded by

govindbirajdar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Spring E-Mail Support:::

In spring you can send the email.via MailSender for that purpose

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. Heres a Maven build project to use Springs JavaMailSenderImpl to send an email via Gmail SMTP server.
1. Project dependency

Add the JavaMail and Springs 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>coms</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. Springs Mail Sender

A Java class to send email with the Springs MailSender interface. File : MailMail.java
package com; 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

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>

Gmail Configuration Details

Configuring other mail clients


The Quick Answer

Follow the instructions below to set up POP1 access in most POP clients. Google Apps users, please follow the default instructions unless otherwise noted, replacing 'your_domain.com' with your actual domain2 name.

We offer instructions for configuring some clients not in our supported client list. If you encounter difficulties, we suggest contacting your mail client's customer support department -we're unable to provide assistance for clients not in our supported POP client list. Standard configuration instructions:
1. Enable POP in Gmail. Don't forget to click Save Changes when you're done. 2. Configure your client to match the settings below:
Incoming Mail (POP3) Server requires SSL: pop.gmail.com Use SSL: Yes Port: 995

Outgoing Mail (SMTP) Server requires TLS3 or SSL:

smtp.gmail.com (use authentication) Use Authentication: Yes Port for TLS/STARTTLS: 587 Port for SSL: 465 your full email address (including @gmail.com or @your_domain.com) your email address ([email protected] or username@your_domain.com) your Gmail password

Account Name:

Email Address:

Password:

Unless you're using recent mode to download mail to multiple clients, make sure you've opted not to leave messages on the server. Your POP settings in Gmail settings are what determines whether or not messages stay on the server, so this setting in your client won't affect how Gmail handles your mail. If your client does not support SMTP4 authentication, you won't be able to send mail through your client using your Gmail address. If you're having trouble sending mail but you've confirmed that encryption is active for SMTP in your mail client, try to configure your SMTP server on a different port (465 or 587).
1. POP: POP (Post office protocol) is a one-way download of your messages that allows you to access your mail with a mail program like Outlook Express or Apple Mail. POP only offers oneway communication, which means that actions you take in the mail program (like marking a message as read) wont be synced to Gmail. 2. domain: A domain is a name for an IP address and is more commonly recognized as a website or web address. For example, Google.com is a domain. 3. TLS: TLS (Transport Layer Security) is a way of changing data such as your username and password into code as it travels across the Internet, so that the data will be secure and private. With mail delivery, TLS begins with an unsecured connection to the mail servers, and then upgrades to a secure connection once information is sent. 4. SMTP: SMTP (Simple Mail Transfer Protocol) is a set of standard Internet procedures by which two email providers (ex. Gmail, Yahoo Mail), transfer email messages to one anothers mail servers.

4. Run it
package com; 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("[email protected]", "[email protected]", "Testing123", "Testing only \n\n Hello Spring Email Sender"); } }

Spring Define an E-mail template in bean configuration file


File : MailMail.java
package com; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class MailMail { private MailSender mailSender; private SimpleMailMessage simpleMailMessage; public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) { this.simpleMailMessage = simpleMailMessage; } public void setMailSender(MailSender mailSender) {

this.mailSender = mailSender; } public void sendMail(String dear, String content) { SimpleMailMessage message = new SimpleMailMessage(simpleMailMessage); message.setText(String.format( simpleMailMessage.getText(), dear, content)); mailSender.send(message); } }

3. Bean configuration file

Define the email template customeMailMessage and mail sender details in the bean configuration file. 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.MailMail"> <property name="mailSender" ref="mailSender" /> <property name="simpleMailMessage" ref="customeMailMessage" /> </bean> <bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage"> <property <property <property <property name="from" value="[email protected]" /> name="to" value="[email protected]" /> name="subject" value="Testing Subject" /> name="text">

<value> <![CDATA[ Dear %s, Mail Content : %s ]]> </value> </property> </bean> </beans>

Spring Sending e-mail with attachment


File : MailMail.java
package com.mkyong.common; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import import import import import org.springframework.core.io.FileSystemResource; org.springframework.mail.MailParseException; org.springframework.mail.SimpleMailMessage; org.springframework.mail.javamail.JavaMailSender; org.springframework.mail.javamail.MimeMessageHelper;

public class MailMail { private JavaMailSender mailSender; private SimpleMailMessage simpleMailMessage; public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) { this.simpleMailMessage = simpleMailMessage; } public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } public void sendMail(String dear, String content) { MimeMessage message = mailSender.createMimeMessage(); try{ MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(simpleMailMessage.getFrom());

helper.setTo(simpleMailMessage.getTo()); helper.setSubject(simpleMailMessage.getSubject()); helper.setText(String.format( simpleMailMessage.getText(), dear, content)); FileSystemResource file = new FileSystemResource("C:\\log.txt"); helper.addAttachment(file.getFilename(), file); }catch (MessagingException e) { throw new MailParseException(e); } mailSender.send(message); } }

3. Bean configuration file

Configure the mailSender bean, email template and specify the email details for the Gmail SMTP server. 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.MailMail"> <property name="mailSender" ref="mailSender" /> <property name="simpleMailMessage" ref="customeMailMessage" /> </bean> <bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage"> <property <property <property <property name="from" value="[email protected]" /> name="to" value="[email protected]" /> name="subject" value="Testing Subject" /> name="text">

<value> <![CDATA[ Dear %s, Mail Content : %s ]]> </value> </property> </bean> </beans>

4. Run it
package com; 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("Yong Mook Kim", "This is text content"); } }

You might also like