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.

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");
 
}
}

referenc from:http://www.mkyong.com/spring/spring-sending-e-mail-via-gmail-smtp-server-with-mailsender/

Spring – Sending E-Mail Via Gmail SMTP Server With MailSender--reference的更多相关文章

  1. java mail 使用 gmail smtp 发送邮件

    smtp 服务器:smtp.gmail.com 使用ssl的端口:465 用户名:username@gmail.com 密码:password** 基本配置没有问题,关键在于Google对安全性要求非 ...

  2. Spring通过Gmail SMTP服务器MailSender发送电子邮件

    Spring提供了一个有用的“org.springframework.mail.javamail.JavaMailSenderImpl”类,通过JavaMail API 简化邮件发送过程.这里有一个项 ...

  3. 邮件发送失败问题:Sending the email to the following server failed : smtp.qiye.163.com:25

    [邮件发送错误] : Sending the email to the following server failed : smtp.qiye.163.com:25, {}org.apache.com ...

  4. SSRS1:配置SMTP Server发送mail

    为了使用SSRS发送mail,必须为Reporting service配置SMTP Server. 1,在Reporting Service Configuration Manager中配置Email ...

  5. phpmailer使用gmail SMTP的方法

    终于能够通过phpmailer使用gmail账号发送邮件了phpmailer(现在的版本是1.73)是一个很好用的工具,可以很方便的使用php语言发送邮件,支持smtp及验证,我们一直都用它. 但是, ...

  6. Linux 上使用 Gmail SMTP 服务器发送邮件通知

    导读 假定你想配置一个 Linux 应用,用于从你的服务器或桌面客户端发送邮件信息.邮件信息可能是邮件简报.状态更新(如 Cachet).监控警报(如 Monit).磁盘时间(如 RAID mdadm ...

  7. Check SMTP Server Availability for ORA-29278 or ORA-29279 errors using UTL_SMTP to Send Email

    Check SMTP Server Availability for ORA-29278 or ORA-29279 errors using UTL_SMTP to Send Email. (文档 I ...

  8. 使用Spring的JAVA Mail支持简化邮件发送(转)

    闲来无事,翻看<Spring in Action>,发现Spring集成了对JAVA Mail的支持,有点小激动的看了一遍,嗯,话说真的简单了很多. Spring的邮件发送的核心是Mail ...

  9. 基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合

    在上一篇<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>中完成了使用JPA对实体数据的CRUD操作. 那么,有些情况,会把一些查询语句写在存储过程中,由 ...

随机推荐

  1. Markdown简单语法总结

    . 标题 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 2. 列表 无序列表 * 西瓜 * 葡萄 * 香蕉 - 西瓜 - 葡萄 - ...

  2. js中replace用法

    js中replace的用法 replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则 ...

  3. wcf教程

    WCF Tutorial WCF stands for Windows Communication Foundation. It is a framework for building, config ...

  4. 利用 Dolby® Digital Plus 提供优质音频体验

    John Deutscher Azure媒体服务首席项目经理 随着媒体设备的增多,一项日益增长的需求是,视频流服务能够向用户提供超高音频质量和具有 5.1 环绕音响的优质内容.通过 Azure媒体 ...

  5. Android的string-array数据源简单使用

    在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的arrays.xml文件里 <?xml version= ...

  6. openlayer调用geoserver发布的地图实现地图的基本功能

    转自:http://starting.iteye.com/blog/1039809 主要实现的功能有放大,缩小,获取地图大小,平移,线路测量,面积测量,拉宽功能,显示标注,移除标注,画多边形获取经纬度 ...

  7. 转载--配置WAMP开发环境

    转自:http://www.cnblogs.com/cardon/archive/2009/12/13/1622935.html 本例安装文件在这里下载       apache2.2.4 MySQL ...

  8. FZU 2092 收集水晶 dp+bfs

    定义dp[t][x1][y1][x2][y2]为在t时刻,人走到x1,y1,影子走到x2,y2所获得最大价值 最终就是所有的dp[max][..][..][..][..]的最大值 然后递推也很自然,枚 ...

  9. HDU 2647

    思路:拓扑排序 #include<stdio.h> #include<string.h> typedef struct { int to; int next; }EdgeNod ...

  10. Windows下搭建Nginx实现负载均衡

    环境:本次测试,使用两台电脑,分别是 192.168.0.1,192.168.0.2. 其中Nginx也部署在 192.168.0.1 电脑上,所以 PC1 的IIS端口不能使用80,因为Nginx需 ...