springboot2.0.4使用邮件服务
添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置文件application.yml:
spring:
mail:
host: smtp.qq.com
port: 465
username: .....@qq.com #开启了SMTP服务的邮箱地址
password: ndnhloztaetcbaej #开启SMTP服务的邮箱地址所提供的授权码,
default-encoding: UTF-8
properties:
mail:
smtp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
debug: true
创建邮件服务类:
@Component
public class MailService {
@Autowired
JavaMailSender javaMailSender;
public void SendSimpleMail(String from ,String to ,String cc,String subject,String content){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(from);//发送人地址:配置文件的邮箱账号
simpleMailMessage.setTo(to);//接收方邮箱地址
simpleMailMessage.setCc(cc);//抄送人
simpleMailMessage.setSubject(subject);//邮件标题
simpleMailMessage.setText(content);//邮件内容
javaMailSender.send(simpleMailMessage);//发送
}
}
测试类依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
测试类代码:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
@Autowired
MailService mailService;
@Test
public void testSimpleMailSend(){
String from ="abcde@qq.com";//发件人地址,配置文件配置的邮箱地址
String to = "abcdef@163.com";//收件人地址
String cc = "abcdefg@qq.com";//抄送人地址
String subject = "邮件主题";
String text = "邮件内容";
mailService.SendSimpleMail(from,to,cc,subject,text);
}
}
发送图片和附件的邮件:
邮件服务类添加方法:
/**
* 带附件的图片和附件的
* 支持多个收件人,抄送人,和附件
*/
public void sendMail(String from, String [] to , String [] cc, String subject, String text, File [] file,String[]srcPath,String []cid){
if(srcPath.length!=cid.length) {
System.out.println("发送失败");
return ;
}
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
if(to!=null){
helper.setTo(to);//重载:数组和字符串,多个收件人
}
if(cc!=null){
helper.setCc(cc);//重载:数组字符串,多个抄送人
}
helper.setSubject(subject);
helper.setText(text,true);//使用图片时,需要cid,所以需要自定义html
for(File f : file){
helper.addAttachment(f.getName(),f);//附件
}
for(int i=0;i<srcPath.length;i++){
FileSystemResource res = new FileSystemResource(new File(srcPath[i]));//图片资源
helper.addInline(cid[i],res);//图片名称和资源
}
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
测试类代码:
@Test
public void testSimpleMailSendImg(){
String from ="abcdef@qq.com";
String to = "abcdefg@163.com";
String subject = "hello images!";
File file = new File("C:\\Users\\administrater\\Desktop\\使用说明.txt");//附件
String [] imgs = new String []{"C:\\Users\\administrater\\Desktop\\1.jpg",
"C:\\Users\\administrater\\Desktop\\2.png"};//图片
String [] cids = new String []{"p01","p02"};
mailService.sendMailWithImg(from,new String []{to},null,subject,createHtml(),new File[]{file},imgs,cids);
}
private String createHtml(){
StringBuilder html = new StringBuilder();
html.append("<div>hello,这是一封带图片资源的邮件");
html.append("这是图片1:<div><img src='cid:p01'/></div>");
html.append("这是图片2:<div><img src='cid:p02'/></div>");
html.append("</div>");
return html.toString();
}