package com.example.mail;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
//这个是发送人的用户名,如1490512620@qq.com
@Value("${spring.mail.username}")
private String from;
//用来发送邮件
@Autowired
private JavaMailSender mailSender;
/**
* @return void
* @Description //TODO 发送简单的文本文件,to:收件人 subject:主题 content:内容
* @Date 11:00 2018/10/30
* @Param [to, subject, content]
**/
public void sendSimpleMail (String to, String subject, String content){
//创建一个简单的文本文件实例
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(from);
mailMessage.setTo(to);
mailMessage.setSubject(subject);
mailMessage.setText(content);
mailSender.send(mailMessage);
}
/**
* @return void
* @Description //TODO 发送一个HTML邮件,to:收件人 subject:主题 content:内容
* @Date 15:01 2018/9/30
* @Param [to, subject, content]
**/
public void sentHtmlMail (String to, String subject, String content ) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
//设置成true,会将这封邮件里边的html标签正常处理,否则会被当成文本处理
helper.setText(content, true);
mailSender.send(message);
}
/**
* @return void
* @Description //TODO 发送一个带附件的邮件,to:收件人,subject:主题,content:主题,filePath:附件的路径(测试可以发送文件和图片,其中图片需要点击预览查看)
* 收件人:helper.setTo(to);抄送:helper.setCc(cc);密送:helper.setBcc(bcc);
* @Date 15:26 2018/9/30
* @Param [to, subject, content, filePath]
**/
public void sendAttachmentMail(String to, String subject, String content, String filePath) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
//设置邮件的一些基本信息
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content);
//设置附件
FileSystemResource file = new FileSystemResource(new File(filePath));
String filename = file.getFilename();
helper.addAttachment(filename, file);
//发送邮件
mailSender.send(message);
}
/**
* 发送图片的邮件,该方法发送的图片直接展示出来 不用预览
* @param to
* @param subject
* @param content
* @param rscId
* @param rscPath
*/
public void sendInlineResourceMail(String to, String subject, String content, String rscId, String rscPath) {
MimeMessage message = mailSender.createMimeMessage();
logger.info("静态邮件发送开始:{}, {}, {}, {}, {}", to, subject, content, rscId, rscPath);
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
//构造邮件内部的图片资源
FileSystemResource file = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, file);
//发送邮件
mailSender.send(message);
logger.info("发送静态邮件成功!");
} catch (MessagingException e) {
logger.error("发送静态邮件失败", e);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot实现邮件发送
共26个文件
java:3个
xml:3个
class:3个
1星 需积分: 50 27 下载量 7 浏览量
2018-10-30
15:48:37
上传
评论
收藏 64KB ZIP 举报
温馨提示
springboot实现邮件发送,包含发送简单的文本文件,发送一个HTML邮件,发送一个带附件的邮件,发送图片的邮件和模板邮件的测试用例
资源推荐
资源详情
资源评论
收起资源包目录
demo.zip (26个子文件)
demo
pom.xml 2KB
.springBeans 470B
target
test-classes
com
example
demo
DemoApplicationTests.class 3KB
classes
META-INF
maven
com.example
demo
pom.xml 2KB
pom.properties 210B
MANIFEST.MF 313B
application.yml 242B
templates
mailTemplete.html 385B
com
example
mail
MailService.class 4KB
DemoApplication.class 693B
demo
mvnw 6KB
.settings
org.eclipse.m2e.core.prefs 97B
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.core.resources.prefs 155B
org.eclipse.jdt.core.prefs 243B
src
test
java
com
example
demo
DemoApplicationTests.java 2KB
main
resources
application.yml 242B
templates
mailTemplete.html 385B
static
java
com
example
mail
MailService.java 4KB
DemoApplication.java 299B
.project 1KB
.classpath 1KB
.gitignore 268B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
mvnw.cmd 5KB
共 26 条
- 1
资源评论
- DTcloud_ysj2021-06-15垃圾 很少的代码 都是基础的代码 没有参考价值
_陌子曦
- 粉丝: 91
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功