Spring JavaMailSender发送邮件
来源:互联网 发布:ubuntu 解压缩 编辑:程序博客网 时间:2024/06/05 07:00
需要要spring包和javax.maill包
配置文件:applicationContext.properties
#mail config
mail.protocol=smtpmail.host=smtp.qq.com
mail.port=465
mail.username=XXXXX@qq.com
mail.name=XXXXX系统
mail.password=********
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.timeout=25000
#mail.smtp.socketFactory.class=javax.Net.ssl.SSLSocketFactory
spring xml文件:
<!-- 读取属性文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application.properties" /> </bean> <!--邮件服务器 --><bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="protocol" value="${mail.protocol}" /><property name="host" value="${mail.host}" /><property name="port" value="${mail.port}" /><property name="username" value="${mail.username}" /><property name="password" value="${mail.password}" /><property name="javaMailProperties"><props><prop key="mail.smtp.auth">${mail.smtp.auth}</prop><prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop><prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop><prop key="#mail.smtp.socketFactory.class">${#mail.smtp.socketFactory.class}</prop></props></property></bean><!-- 异步线程执行器 --><bean id="taskExecutor"class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"><property name="corePoolSize" value="10" /><property name="maxPoolSize" value="30" /></bean>
mail服务接口:
import java.io.IOException;import java.util.List;import javax.mail.MessagingException;import javax.mail.internet.InternetAddress;/** * 邮件 * * @author Shi Zezhu * @date 2017年7月14日 上午11:22:52 */public interface MailService {/** * 发送邮件 * * @param to 收件 * @param cc 抄送 * @param subject 主题 * @param htmlflag 是否HTLM内容 * @param content 内容 * @author Shi Zezhu * @date 2017年7月14日 下午4:35:50 */void sendMail(InternetAddress[] to, InternetAddress[] cc, String subject, boolean htmlflag,String content) MessagingException, IOException;/** * 发送邮件 * * @param to 收件 * @param cc 抄送 * @param subject 主题 * @param content 内容 * @param htmlflag 是否HTLM内容 * @author Shi Zezhu * @date 2017年7月14日 下午4:35:50 */void sendMail(String[] to, String[] cc, String subject, String content, boolean htmlflag)throws MessagingException, IOException;/** * 发送邮件 * * @param to 收件 * @param subject 主题 * @param htmlflag 是否HTLM内容 * @param content 内容 * @throws MessagingException * @throws IOException * @author Shi Zezhu * @date 2017年7月15日 上午11:31:35 */void sendMail(List<InternetAddress> to, String subject, boolean htmlflag, String content)throws MessagingException, IOException; /** * 发送邮件 * * @param to 收件 * @param subject 主题 * @param htmlflag 是否HTLM内容 * @param content 内容 * @throws MessagingException * @throws IOException * @author Shi Zezhu * @date 2017年7月15日 上午11:31:35 */ void sendMail(InternetAddress to, String subject, boolean htmlflag, String content) throws AjaxResultException, MessagingException, IOException;/** * 发送邮件 * * @param to 收件 * @param cc 抄送 * @param subject 主题 * @param htmlflag 是否HTLM内容 * @param content 内容 * @author Shi Zezhu * @date 2017年7月14日 下午4:35:50 */void sendMail(List<InternetAddress> to, List<InternetAddress> cc, String subject, boolean htmlflag,String content) throws MessagingException, IOException;/** * 发送邮件 * * @param to 收件 * @param subject 主题 * @param content 内容 * @param htmlflag 是否HTLM内容 * @throws MessagingException * @throws IOException * @author Shi Zezhu * @date 2017年7月15日 上午11:32:02 */void sendMail(List<String> to, String subject, String content, boolean htmlflag)throws MessagingException, IOException; /** * 发送邮件 * * @param to 收件 * @param subject 主题 * @param content 内容 * @param htmlflag 是否HTLM内容 * @throws MessagingException * @throws IOException * @author Shi Zezhu * @date 2017年7月15日 上午11:32:02 */ void sendMail(String to, String subject, String content, boolean htmlflag) throws AjaxResultException, MessagingException, IOException; /** * 发送邮件 * * @param to 收件 * @param cc 抄送 * @param subject 主题 * @param content 内容 * @param htmlflag 是否HTLM内容 * @author Shi Zezhu * @date 2017年7月14日 下午4:35:50 */void sendMail(List<String> to, List<String> cc, String subject, String content,boolean htmlflag) throws MessagingException, IOException;}
mail服务实现:
import java.io.IOException;import java.util.List;import javax.annotation.Resource;import javax.mail.MessagingException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import org.apache.commons.lang3.StringUtils;import org.springframework.core.task.TaskExecutor;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Service;;import cn.szz.security.service.MailService;import cn.szz.security.utils.ErrorCode;import cn.szz.security.utils.PropertiesUtils;@Servicepublic class MailServiceImpl implements MailService {@Resourceprivate JavaMailSender mailSender;@Resourceprivate TaskExecutor taskExecutor;@Overridepublic void sendMail(InternetAddress[] to, InternetAddress[] cc, String subject, boolean htmlflag,String content) throws AjaxResultException, MessagingException, IOException {if (to == null || to.length == 0) {return;}if (cc == null) {cc = new InternetAddress[]{};}if (StringUtils.isBlank(subject)) {return;}if (StringUtils.isBlank(content)) {return;}MimeMessage mime = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mime, true, "UTF-8");helper.setFrom(new InternetAddress(PropertiesUtils.getProperty("mail.username"), PropertiesUtils.getProperty("mail.name"), "UTF-8"));helper.setTo(to);helper.setCc(cc);helper.setSubject(subject);helper.setText(content, htmlflag);this.sendMail(mime);}@Overridepublic void sendMail(String[] to, String[] cc, String subject, String content,boolean htmlflag) throws AjaxResultException, MessagingException, IOException {if (to == null || to.length == 0) {return;}if (cc == null) {cc = new String[]{};}if (StringUtils.isBlank(subject)) {return;}if (StringUtils.isBlank(content)) {return;}MimeMessage mime = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mime, true, "UTF-8");helper.setFrom(PropertiesUtils.getProperty("mail.username"), PropertiesUtils.getProperty("mail.name"));helper.setTo(to);helper.setCc(cc);helper.setSubject(subject);helper.setText(content, htmlflag);this.sendMail(mime);}@Overridepublic void sendMail(List<InternetAddress> to, String subject, boolean htmlflag,String content) throws AjaxResultException, MessagingException, IOException {this.sendMail(to.toArray(new InternetAddress[to.size()]), null, subject, htmlflag, content);} @Override public void sendMail(InternetAddress to, String subject, boolean htmlflag, String content) throws AjaxResultException, MessagingException, IOException { this.sendMail(new InternetAddress[]{to}, null, subject, htmlflag, content); } @Overridepublic void sendMail(List<InternetAddress> to, List<InternetAddress> cc, String subject, boolean htmlflag,String content) throws AjaxResultException, MessagingException, IOException {this.sendMail(to.toArray(new InternetAddress[to.size()]), cc.toArray(new InternetAddress[cc.size()]), subject, htmlflag, content);}@Overridepublic void sendMail(List<String> to, String subject, String content,boolean htmlflag) throws AjaxResultException, MessagingException, IOException {this.sendMail(to.toArray(new String[to.size()]), null, subject, content, htmlflag);} @Override public void sendMail(String to, String subject, String content, boolean htmlflag) throws AjaxResultException, MessagingException, IOException { this.sendMail(new String[]{to}, null, subject, content, htmlflag); }@Overridepublic void sendMail(List<String> to, List<String> cc, String subject, String content,boolean htmlflag) throws AjaxResultException, MessagingException, IOException {this.sendMail(to.toArray(new String[to.size()]), cc.toArray(new String[cc.size()]), subject, content, htmlflag);}private void sendMail(final MimeMessage mime) {taskExecutor.execute(new Runnable() {public void run() {mailSender.send(mime);}});}}
阅读全文
2 0
- Spring JavaMailSender发送邮件
- Spring JavaMailSender发送邮件
- spring 实现邮件发送 JavaMailSender
- 使用spring的JavaMailSender发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- Spring Boot 配置 JavaMailSender发送邮件
- 使用spring的JavaMailSender发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- 使用spring的JavaMailSender发送邮件
- 在Spring Boot中使用JavaMailSender发送各类邮件
- springboot【27】邮件发送JavaMailSender
- springMail的JavaMailSender和MimeMessagePreparator发送邮件
- freemarker+javaMailSender+springmvc实现邮件发送
- springMail的JavaMailSender和MimeMessagePreparator发送邮件
- SpringBoot JavaMailSender发送邮件(QQ和163邮箱)
- a 标签和button按钮返回上个页面
- C# 根据Url获取页面所有内容
- 机器学习&数据挖掘笔记
- 通过AndroidManifest中的meta-data标签来设置log开关
- R语言 生成图片 存储图片 存储图片格式 存储位置
- Spring JavaMailSender发送邮件
- python学习(基础补充)--字符串、列表、元组
- SQLite学习手册(C/C++接口简介)
- 位运算训练04—输入一个整数a,再输入两个整数p(p<32),v(0|1);将该整数a的p位设置为v,输出修改后的该整数的二进制表示
- Intellij IDEA 2017 详细图文教程之概述
- 支付宝借呗到底上不上征信?
- hdfs心跳机制
- NYOJ- 737 石子合并(一)(dp+多种方法实现)
- 用 Unity 和 HTC Vive 实现高级 VR 机制(1)