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. Android Button悬浮在SurfaceView上

    实现Button悬浮于与SurfaceView之上实现 注意:你实现的SurfaceView和android中的Button,EditView是同级的,不能把一个包含在另一个里面 1.创建自己的Sur ...

  2. WCF - IIS Hosting

    WCF - IIS Hosting Hosting a WCF service in IIS (Internet Information Services) is a step-by-step pro ...

  3. BZOJ_3270_博物馆_(高斯消元+期望动态规划+矩阵)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3270 \(n\)个房间,刚开始两个人分别在\(a,b\),每分钟在第\(i\)个房间有\(p[ ...

  4. BZOJ_2002_弹飞绵羊_(LCT)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2002 一列n个数,a[i]表示向后a[i]个,问第k个数进行多少次向后跳跃会飞出去. 分析 i ...

  5. C#基础回顾:正则表达式

    C#基础回顾:正则表达式 写在前面:本文根据笔者的学习体会结合相关书籍资料对正则表达式的语法和使用(C#)进行基本的介绍.适用于初学者. 摘要:正则表达式(Regular Expressions),相 ...

  6. ActiveMQ中的安全机制 [转]

    本文简单介绍ActiveMQ通过JAAS实现的安全机制.JAAS(Java Authentication and Authorization Service)也就是java认证/授权服务.这是两种不同 ...

  7. Prism简介

    Prism是由微软Patterns & Practices团队开发的项目,目的在于帮助开发人员构建松散耦合的.更灵活.更易于维护并且更易于测试的WPF应用或是Silverlight应用以及Wi ...

  8. Django学习随想(1)

    关于Django的模型部分: 模型操作实际上都是针对数据库的一系列操作. Django封装了底层的操作,给用户提供了一组非常python化的模型对象.让python开发者可以很方便.直观地进行数据库表 ...

  9. Clean Code–Chapter 7 Error Handling

    Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...

  10. Java 并发之线程安全

    写线程安全的代码,说白了就是管理一个类的共享的.可变的状态.只要有多于 1 个线程对类的状态进行写入,那么就必须用同步来协调这多个线程对状态的访问.对于一个没有状态的类来说(简单的理解就是只有方法没有 ...