2018年11月10日星期六
随笔 笔记
springboot集成邮件发送
一、导入依赖包
org.springframework.boot
spring-boot-starter-mail
二、开启服务(一个即可)
三、发送短信:配置邮件客户端到1069…获取密码(每个人不一样)
四、配置文件application.yml 等同于application.properties
#发送邮件 层次yml规范
mail:
password: pcchjrrbgifldegb //发送短信到1069...获取的密码
default-encoding: UTF-8
host: smtp.qq.com //smtp.163.com........
properties:
mail:
smtp:
starttls:
enable: true
required: true
auth: true
username:[email protected] //收件人地址邮件
application:
name: spirng-boot-mail
mail:
fromMail:
addr:[email protected] //发件人邮件地址(可不写)
#我的日志
logging:
level:
com:
yyg: DEBUG
五、抽取后的代码
发送邮件附带文件/简单发送邮件
MailService
/**
* 邮件服务接口
* Date 2018/11/10
* @Auther 阳彦刚
*/
public interface MailService {
/**
* 发送带附件的邮件和发送简单邮件 两个接口
* @param to 接受者
* @param subject 标题
* @param content 发送内容
* @param filePath 发送附件
*/
void sendAttachmentsMail(String resiveUser,String subject,String content,String filePath);
void sendMail(String resiveUser,String subject,String content);
}
MailServiceImpl
package com.yyg.api.impl;
import java.io.File;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
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 com.alibaba.dubbo.config.annotation.Service;
import com.yyg.api.MailService;
import com.yyg.utils.EmailSendUtils;
/**
* 邮件服务类
* Date 2018/11/10
* @Auther 阳彦刚
*/
@Service //交给spring创建bean
public class MailServiceImpl implements MailService {
/**
* 发送带附件的邮件和发送简单邮件
* @param to 接受者
* @param subject 主题
* @param content 内容
* @param filePath 文件路径
*/
@Autowired
EmailSendUtils emailSendUtils;
@Override
public void sendAttachmentsMail(String resiveUser, String subject, String content, String filePath) {
emailSendUtils.sendAttachmentsMail(resiveUser, subject, content, filePath);
}
@Override
public void sendMail(String resiveUser, String subject, String content) {
emailSendUtils.sendMail(resiveUser, subject, content);
}
}
工具类EmailUtils
/**
*Date 2018/11/10
*@Auther 阳彦刚
*/
package com.yyg.utils;
import java.io.File;
import javax.mail.internet.MimeMessage;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component //交给spring创建bean
public class EmailSendUtils {
@Autowired
private JavaMailSender mailSender; //导入Mail.jar包自带的类,不用管
// 获取application.yml的值
@Value("${mail.fromMail.addr}")
private String form; //发件人邮箱地址
// 发送邮件 附带文件
@Async
public void sendAttachmentsMail(String resiveUser, String subject, String content, String filePath) {
//创建微型邮件对象
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(form); //发件人邮件地址
helper.setTo(resiveUser); //收件人邮箱地址
helper.setSubject(subject); //邮箱标题
helper.setText(content);//邮件内容
//获取本地文件从文件输入流
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
// 添加多个附件可以使用多条
helper.addAttachment(fileName, file);
mailSender.send(message);
System.out.println("带附件的邮件发送成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("发送带附件的邮件失败");
}
}
//简单发送邮件 接上
@Async //可用不管 用于集成Dubbo 消费优化的注解
public void sendMail(String to, String subject, String content) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(form);// 发起者
mailMessage.setTo(to);// 接受者
mailMessage.setSubject(subject);//发送邮件的标题
mailMessage.setText(content);//发送内容
try {
mailSender.send(mailMessage);
System.out.println("发送简单邮件");
} catch (Exception e) {
System.out.println("发送简单邮件失败" + e.getMessage());
}
}
}
六、测试
package com.yyg.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.yyg.EmailRunApp;
import com.yyg.api.MailService;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=EmailRunApp.class)
public class testEmailSend {
@Autowired
MailService mailService;
//附带文件发送邮件测试
@Test
public void testSend()throws Exception{
String filePath="D:\\谷歌下载\\a.jpg";
mailService.sendAttachmentsMail("[email protected]", "附录邮件", "昔年残存终不负,化作巨龙腾飞",filePath);
}
//简单发送邮件测试
@Test
public void testSend02()throws Exception{
mailService.sendMail("[email protected]", "普通发送邮件", "昔年残存终不负,化作巨龙腾飞");
}
}
有疑问的欢迎联系2822256710,加v :yygtq0818126,欢迎点评