Spring提供了一个有用的“org.springframework.mail.javamail.JavaMailSenderImpl”类,通过JavaMail API 简化邮件发送过程。这里有一个项目中使用Spring “JavaMailSenderImpl”通过Gmail SMTP服务器发送电子邮件。
1. Spring邮件发件人
Java 类使用 Spring 的 MailSender 接口发送电子邮件。

File : MailMail.java

package com.yiibai.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);
}
}
2. bean配置文件
配置 mailSender bean 并指定Gmail的SMTP服务器电子邮件的详细信息。

Gmail的配置细节(这里是墙,该翻的翻) – 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="yiibai.com@gmail.com" />
<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.yiibai.common.MailMail">
<property name="mailSender" ref="mailSender" />
</bean> </beans>

运行它

package com.yiibai.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"); }
}
 
下载源代码 –  http://pan.baidu.com/s/1gepbWEf

Spring通过Gmail SMTP服务器MailSender发送电子邮件的更多相关文章

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

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

  2. 在Python中使用SMTP发送电子邮件

    Python中有内置的smtplib模块,完成一封邮件的发送,需要做很多准备工作.第三方的py_smtp这个包也是基于smtplib的,在Python中使用py_smtp发送电子邮件非常方便,只要填写 ...

  3. C# 发送电子邮件(含附件)用到的类 system.web.mail

    主要是用到了System.Web.Mail命名空间,用到了此空间的三个类,分别是: ●MailMessage类,用于构造电子邮件●MailAttachment类,用于构造电子邮件附件●SmtpMail ...

  4. JAVA实现发送电子邮件

    相信大家对于网站也好,手机app也好,用户注册时,需要进行邮箱验证的功能特别好奇吧,本篇我将带领大家一起实现一下这个简单而又神奇的小功能,让我们的应用也可以加入这些神奇的元素.废话不多说,下面开始我们 ...

  5. CentOS7配置mailx使用外部smtp服务器发送邮件

    转自huskiesir的博客: 发送邮件的两种方式: 1.连接现成的smtp服务器去发送(此方法比较简单,直接利用现有的smtp服务器比如qq.新浪.网易等邮箱,只需要直接配置mail.rc文件即可实 ...

  6. centeros 7配置mailx使用外部smtp服务器发送邮件

    发送邮件的两种方式: 1.连接现成的smtp服务器去发送(此方法比较简单,直接利用现有的smtp服务器比如qq.新浪.网易等邮箱,只需要直接配置mail.rc文件即可实现) 2.自己搭建私有的smtp ...

  7. 转: "SMTP 服务器无法识别在 Mac 2011 Outlook 中发送电子邮件时错误。错误 17092"

    在 Mac 2011 Outlook 中发送电子邮件时,您可能会收到下面的错误消息: 无法发送邮件.SMTP 服务器无法识别任何 Outlook 所支持的身份验证方法.请尝试更改帐户设置中的 SMTP ...

  8. Spring发送电子邮件

      Spring提供了发送电子邮件的功能,它向用户屏蔽了底层邮件系统的一些细节,同时代表客户端负责底层的资源处理. Spring的邮件服务支持主要是通过JavaMailSender这个接口实现的: p ...

  9. Spring – Sending E-Mail Via Gmail SMTP Server With MailSender--reference

    Spring comes with a useful ‘org.springframework.mail.javamail.JavaMailSenderImpl‘ class to simplify ...

随机推荐

  1. Python os模块和sys模块 操作系统的各种接口

    一.os模块 这个模块提供了一个便携式去使用操作系统的相关功能,如果只是想操作路径,请参阅os.path模块. ''' os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 ...

  2. 转:在CentOS 7.3使用yum安装 MySql5.6.24

    按照CentOS 6.5的方法在CentOS 7上安装会失败,需要按照下文进行安装. 1.安装rpm包 Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无 ...

  3. docvalue and fielddata

    大部分字段类型默认被索引的(inverted index),可以被搜索search: 哪个文档有这个词sort&aggregations: look up the document and f ...

  4. jekyll安装及使用

    清除旧版Ruby,若存在 yum remove ruby 若为源码,使用如下命令 cd <your-ruby-source-path> make uninstall 下面开始安装Ruby ...

  5. 删除WP提示:自动升级WordPress失败

    wordpress后台总有烦人的升级失败的提示,查了半天找不到怎么去掉:“自动升级WordPress失败--请再试一次”这个提示的方法,特意分享出来 方法/步骤   1 打开wordpress根目录找 ...

  6. git团队开发

    用git有一年了,下面是我这一年来的git使用总结,覆盖了日常使用中绝大多数的场景.嗯,至少是够用一年了,整理出来分享给大家,不明白的地方可以回复交流. git设置关闭自动换行 git config ...

  7. Dockerfile 备份

    dotnet core app FROM microsoft/dotnet:1.1.0-runtime WORKDIR /mvcApp COPY ./out . ENTRYPOINT ["d ...

  8. node+express+socket.io+mysql=通讯服务器搭建(一)

    首发github/blog 欢迎大家评论给星 安装 首先假定你已经安装了 Node.js,接下来为你的应用创建一个目录,然后安装express-generator应用骨架 $ mkdir node-d ...

  9. merc_timer_handle_t函数的使用

    merc_timer_handle_t,是定义一个时间类型,这个时间类型可以用来接收2个函数之间的wasted time 但是在项目中出现这个情况: 因为在脚本中添加了该函数:

  10. 什么是泛型 转载自http://www.blogjava.net/Jack2007/archive/2008/05/05/198566.html

    我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为方法的参数类 ...