使用Spring的MailSender发送邮件
第1步:扫描邮件发送的属性配置
<context:property-placeholder location="/config/mail.properties" ignore-unresolvable="true" />
mail.properties
mailServerHost=your host
mailServerPort=25
mailUserName= your name
mailPassword= your password
mailFromAddress= xijinping@china.com
第2步:配置bean
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>${mailServerHost}</value>
</property>
<property name="port">
<value>${mailServerPort}</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
<property name="username">
<value>${mailUserName}</value> <!-- 发送者用户名 -->
</property>
<property name="password">
<value>${mailPassword}</value> <!-- 发送者密码 -->
</property>
<!-- <property name="from">
<value>${mailFromAddress}</value>
</property> -->
</bean>
第3步:注入bean
@Service
public class MailService {
@Resource
private JavaMailSender mailSender;
@Value("${mailFromAddress}")
private String mailFromAddress;
public void send(String subject,String content,String to){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
simpleMailMessage.setFrom(mailFromAddress);
simpleMailMessage.setTo(to);
mailSender.send(simpleMailMessage);
}
}
第4步:调用API发送
mailService.send();
注意事项:
需要特别注意,userName是用来连接服务器的,from参数是可以手动设置的。
from和userName可以不同。
from参数也是必须的,通过@Value注解注入到Java代码中。
使用Spring的MailSender发送邮件的更多相关文章
- 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- Spring Boot 2发送邮件手把手图文教程
原文:http://www.itmuch.com/spring-boot/send-email/ 本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有 ...
- Spring整合freemarker发送邮件
转载:http://blog.csdn.net/zdp072/article/details/32745335 分类: freemarker spring 2014-06-20 23:39 752人阅 ...
- 使用spring的JavaMail发送邮件
以前我们使用JavaMail发送邮件,步骤挺多的.现在的项目跟Spring整合的比较多.所以这里主要谈谈SpringMail发送. 导入jar包. 配置applicationContext-email ...
- Spring Boot异步发送邮件和请求拦截器配置
用户登录流程图: 在spring拦截器中进行鉴权操作: 控制器的拦截: import com.mooc.house.common.model.User; import org.springframew ...
- 通过Spring Mail Api发送邮件
使用Java Mail API来发送邮件也很容易实现,但是最近公司一个同事封装的邮件API实在让我无法接受,于是便打算改用Spring Mail API来发送邮件,顺便记录下这篇文章. [Spring ...
- Spring Quartz结合Spring mail定期发送邮件
文件配置例如以下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- 使用spring的JavaMailSender发送邮件
一:pom.xml <!-- java邮件 --> <dependency> <groupId>javax.mail</group ...
- 通过spring实现javamail发送邮件功能
以前很早的时候大家都用javamail实现发送邮件的功能,而且我们也一直沿用至今,代码拷过来用用就行了,现在我们改为用spring来实现,这样一来减少代码的复杂度,也能更好的契合spring理念 首先 ...
随机推荐
- spring5.0新特性
spring5.0新特性 学习了:http://blog.csdn.net/u012562943/article/details/77449666 https://www.cnblogs.com/xu ...
- uva 10479(找规律+递归)
题意:有一个初始序列第一个数字是0. 规律是把前一次推出来的每个数字x.先接x个0,然后接x+1. 0 –> 1 –> 02 –> 1003 –> 02110004 那么这个序 ...
- IOS - 查找未使用的图片
实现细节都在代码里面, 帮助 -h. # -*- coding: utf-8 -*- """ 检查IOS应用图片是否使用 1. 读取有效文件: 图片(.png, .jpg ...
- python为在线漫画站点自制非官方API(未完待续)
接下来将记录我一步一步写一个非官方API的过程,由于一些条件的约束,最后的成品可能非常粗暴简陋 如今介绍要准备的全部工具: 系统:ubuntu 14.04 语言:python 2.7 须要自行安装的库 ...
- JS遮罩层
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- python spark 求解最大 最小 平均
rdd = sc.parallelizeDoubles(testData); Now we’ll calculate the mean of our dataset. 1 LOGGER.info( ...
- Thymeleaf:工具对象用法
转自:https://blog.csdn.net/mygzs/article/details/52668248 #dates /* * ================================ ...
- 更换WordPress编辑器为TinyMCE Advanced
WordPress自带的编辑器功能很少,连更换字体样式大小都不行,没关系WordPress的插件中心插件非常多 在插件中心搜索TinyMCE Advanced 安装启用 还没完 点击设置 里面有丰富的 ...
- HDL之Bitwise operation
1 Verilog 1.1 Bitwise operator Bitwise operators perform a bit wise operation on two operands. They ...
- OnLineML一:关于Jubatus 的简介...
一:简介:原文链接:jubat.us/en/ xuwenq.iteye.com/blog/1702746 Jubatus http://jubat.us/en/overview.html 是一个面向 ...