springboot发送邮件
来源:互联网 发布:美国历史书籍推荐 知乎 编辑:程序博客网 时间:2024/06/07 23:03
<?xml version="1.0"?><projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.lance</groupId><artifactId>spring-boot-parent</artifactId><version>1.1</version><relativePath>../spring-boot-parent/pom.xml</relativePath></parent><artifactId>spring-boot-email</artifactId><packaging>war</packaging><name>spring-boot-email</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>19.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.12</version></dependency><!-- common --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.2.1</version></dependency></dependencies><build><finalName>spring-boot-email</finalName></build></project>
//application.properties
# IDENTITY (ContextIdApplicationContextInitializer)spring.application.index=ActiveMQ.v1.1spring.application.name=ActiveMQ Boot#Serverserver.port=80server.jsp-servlet.class-name=org.apache.jasper.servlet.JspServletserver.error.whitelabel.enabled=true#LOGlogging.config=classpath:log4j2.xml# Email (MailProperties)spring.mail.default-encoding=UTF-8spring.mail.host=smtp.qq.comspring.mail.password=123456spring.mail.port=25spring.mail.protocol=smtpspring.mail.test-connection=falsespring.mail.username=server1@qq.comspring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.transport.protocol=smtpsspring.mail.properties.mail.smtps.quitwait=false
package com.lance.email;import org.apache.commons.lang3.StringUtils;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.MailException;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.stereotype.Component;@Component("emailSender")public class EmailSender {private Logger logger = LogManager.getLogger(getClass());private String defaultFrom = "server1@qq.com";@Autowiredprivate JavaMailSender javaMailSender;/** * 发送邮件 * @param to收件人地址 * @param subject邮件主题 * @param content邮件内容 * @author lance */public boolean sender(String to, String subject, String content) {return sender(to, subject, content, true);}/** * 发送邮件 * @param to收件人地址 * @param subject邮件主题 * @param content邮件内容 * @param html是否格式内容为HTML * @author lance */public boolean sender(String to, String subject, String content, boolean html){if(StringUtils.isBlank(to)) {logger.error("邮件发送失败:收件人地址不能为空.");return false;}return sender(new String[]{to}, subject, content, html);}/** * sender message * @param to * @param subject * @param content * @param html * @return */public boolean sender(String[] to, String subject, String content, boolean html){if(to == null || to.length == 0) {logger.error("批量邮件发送失败:收件人地址不能为空.");return false;}SimpleMailMessage simpleMailMessage = new SimpleMailMessage();simpleMailMessage.setFrom(defaultFrom);simpleMailMessage.setTo(to);simpleMailMessage.setSubject(subject);simpleMailMessage.setText(content);try {javaMailSender.send(simpleMailMessage);return true;} catch (MailException e) {logger.error("发送邮件错误:{}, TO:{}, Subject:{},Content:{}.", e, to, subject, content);return false;}}}
阅读全文
0 0
- springBoot发送邮件
- springboot 发送邮件
- SpringBoot发送邮件
- springboot发送邮件
- springboot发送邮件
- springboot发送邮件
- JAVA发送邮件及springboot发送邮件
- yx: SpringBoot发送邮件功能
- SpringBoot之发送邮件功能
- springboot【27】邮件发送JavaMailSender
- springboot 实现邮件发送功能
- SpringBoot中进行邮件发送
- springboot-email 邮件发送实战
- springboot 普通发送邮件 和 freemarker模板发送邮件
- springboot邮件发送功能和两个常见问题
- springboot发送邮件的实现例子
- springboot中发送各种类型的邮件
- SpringBoot-Thymeleaf发送模板带附件邮件
- 10-21 Walk through the forest(最短路预处理+建树)
- map容器的简单操作
- MGD 调试 Lives2d 文字破碎问题 (MX2)
- 2. Add Two Numbers
- 关于@JsonFormat的时区问题
- springboot发送邮件
- 闰年问题(C++面向对象初步)
- 组合模式(Composite Pattern)——管理良好的集合
- Python第一课学习笔记
- redis-AOF
- Android 判断 Intent 是否存在
- 对JAVA集合进行遍历删除时务必要用迭代器
- Java实现中文分词,IKAnalyzer
- springboot + jsp