SpringMVC 实现邮件发送功能
配置spring-mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<!-- 实现邮件服务 -->
<bean id="mimeMessage" class="javax.mail.internet.MimeMessage"
factory-bean="javaMailSender" factory-method="createMimeMessage" />
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com" />
<property name="username" value="hongliang_0510@163.com" /> //你的邮箱
<property name="password" value="***********" />//邮箱密码
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.host">smtp.163.com</prop>
<prop key="mail.smtp.timeout">25000</prop>
<prop key="mail.smtp.port">25</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
</props>
</property>
</bean>
<bean id="mailService" class="core.mail.service.impl.MailServiceImpl">
<property name="mailSender" ref="javaMailSender" />
<property name="mimeMessage" ref="mimeMessage" />
</bean></beans>
--邮件Module实体类
public class MailModel {
private String fromAddress;//发送人地址1个
private String toAddresses;//接收人地址,可以为很多个,每个地址之间用";"分隔,比方说450065208@qq.com;lpf@sina.com
private String subject;//邮件主题
private String content;//邮件文本内容
private String[] attachFileNames;//附件
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getToAddresses() {
return toAddresses;
}
public void setToAddresses(String toAddresses) {
this.toAddresses = toAddresses;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
}
--service 接口
public interface MailService {
public void sendAttachMail(MailModel mail);
}
--service实现类
@Service
public class MailServiceImpl implements MailService{
@Resource
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
private MimeMessage mimeMessage;
public void setMimeMessage(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
private static Logger logger = Logger.getLogger(MailServiceImpl.class);
/**
* 发送html格式的,带附件的邮件
*/
@Override
public void sendAttachMail(MailModel mail) {
try {
MimeMessageHelper mailMessage = new MimeMessageHelper(
this.mimeMessage, true, "UTF-8");
mailMessage.setFrom(mail.getFromAddress());// 设置邮件消息的发送者
mailMessage.setSubject(mail.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
mailMessage.setText(mail.getContent(), true); // 设置邮件正文,true表示以html的格式发送
String[] toAddresses = mail.getToAddresses().split(";");// 得到要发送的地址数组
for (int i = 0; i < toAddresses.length; i++) {
mailMessage.setTo(toAddresses[i]);
/* for (String fileName : mail.getAttachFileNames()) {
mailMessage.addAttachment(fileName, new File(fileName));
}*/
// 发送邮件
this.mailSender.send(this.mimeMessage);
logger.info("send mail ok=" + toAddresses[i]);
}
} catch (Exception e) {
logger.error(e);
e.printStackTrace();
}
}
}
--controller中的发邮件的方法
@RequestMapping(value = "/sendEmail.do")
@ResponseBody
public AjaxUtil sendEmail(HttpServletRequest request) {
String title = request.getParameter("title");
String userName= request.getParameter("userName");
String email = request.getParameter("email");
String advice = request.getParameter("advice");
MailModel mail = new MailModel();
mail.setFromAddress("hongliang_0510@163.com");
mail.setToAddresses("71006957@qq.com");
mail.setSubject(title);
StringBuffer sb = new StringBuffer();
sb.append("姓名:"+userName+"<br/>");
sb.append("邮箱:"+email+"<br/>");
sb.append("建议:"+advice+"<br/>");
mail.setContent(sb.toString());
AjaxUtil ajax=new AjaxUtil();
try{
mailService.sendAttachMail(mail);
}catch(Exception e){
ajax = new AjaxUtil(e.getMessage());
}
return ajax;
}
SpringMVC 实现邮件发送功能的更多相关文章
- SpringBoot 2.X从0到1实现邮件发送功能
Spring中提供了JavaMailSender接口实现邮件发送功能,在SpringBoot2.X中也封装了发送邮件相关的Starter并且提供了自动化配置. 本文目录 一.添加对应的Starter二 ...
- .NET开发邮件发送功能的全面教程(含邮件组件源码)
今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知识 2) ...
- 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇
在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...
- redmine邮件发送功能配置详解
redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...
- .NET开发邮件发送功能
.NET开发邮件发送功能 今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知 ...
- shell邮件发送功能实现
本文中以163邮箱为例,测试shell邮件发送功能.常见的工具有:mailx.sendmail.mutt等. 1.设置邮件客户端 (1)启用pop3.smtp服务,以支持第三方客户端支持 (2)设置授 ...
- spring-boot-route(二十二)实现邮件发送功能
在项目开发中,除了需要短信验证外,有时候为了节省 短信费也会使用邮件发送.在Spring项目中发送邮件需要封装复杂的消息体,不太方便.而在Spring Boot项目中发送邮件就太简单了,下面一起来看看 ...
- System.Net邮件发送功能踩过的坑
System.Net邮件发送功能踩过的坑 目录 System.Net邮件发送功能踩过的坑 1.EazyEmail邮件发送类库 2.邮件发送授权码与邮件密码 3.通过邮件密码来发送邮件 4.Wiresh ...
- 结合ABP源码实现邮件发送功能
1. 前言 2. 实现过程 1. 代码图(重) 2.具体实现 2.1 定义AppSettingNames及AppSettingProvider 2.2 EmailSenderConfiguration ...
随机推荐
- 10 款精美的 CSS3 全新特效
大家都知道,在网页制作时使用CSS技术,可以有效地对页面的布局.字体.颜色.背景和其它效果实现更加精确的控制.只要对相应的代码做一些简单的修改,就可以改变同一页面的不同部分,或者页数不同的网页的外观和 ...
- Android 获取控件相对于屏幕位置
// View宽,高 public int[] getLocation(View v) { int[] loc = new int[4]; int[] location = new int[2]; v ...
- HDU 4286 Data Handler 双向链表/Splay
Data Handler Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- 查询(c语言实现)
/* * Describe:这是一个简单的查询程序 * Date: 2013/12/7 */ #include <stdio.h> #include <stdlib.h> #d ...
- [AngularJS] Best Practise - Module
Module definitions Angular modules can be declared in various ways, either stored in a variable or u ...
- apache2.2 虚拟主机配置
一.改动httpd.conf 打开appserv的安装文件夹,找到httpd.conf文件,分别去掉以下两行文字前面的#号. #LoadModule vhost_alias_module module ...
- Android Studio中导入第三方库
之前开发Android都是使用的eclipse,近期因为和外国朋友Timothy一起开发一款应用,他是从WP平台刚切换使用Android的,使用的开发环境时Android Studio,为了便于项目的 ...
- 使用Linux的mail命令发送邮件
由于经常工作在linux下,所以很多时候需要将自己工作的报告或其他有用的东东发送给相关的人,所以花时间研究了一下在linux下如何发送mail.我们通常能用到下面3中发送方式: 1. 使用Shell当 ...
- Qt组件中的双缓冲无闪烁绘图
双缓冲绘图在Qt4中,所有的窗口部件默认都使用双缓冲进行绘图.使用双缓冲,可以减轻绘制的闪烁感.在有些情况下,用户要关闭双缓冲,自己管理绘图.下面的语句设置了窗口部件的Qt::WA_PaintOn ...
- Web安全 之 SQL注入
随着B/S模式应用开发的发展,使用这种模式编写的应用程序也越来越多.相当大一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据 ...