Spring框架JavaMailSender发送邮件工具类详解

网友投稿 348 2023-01-08


Spring框架JavaMailSender发送邮件工具类详解

本文实例为大家分享了Spring框架javaMailSender发送邮件工具类,供大家参考,具体内容如下

需要用到的jar包:

下面是发送工具类代码:

package com.test.email;

import org.springframework.core.io.FileSystemResource;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;

import org.springframework.mail.javamail.MimeMessageHelper;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.mail.internet.MimeUtility;

import java.util.Arhttp://rayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;

/**

* @author tlimited

* @create 2017-08-17 11:38

**/

public class SendMailUtils {

// Spring的邮件工具类,实现了MailSender和JavaMailSender接口

private JavaMailSenderImpl mailSender;

/**

* 初始化邮件发送数据

* @param host 服务器

* @param username 发送人

* @param passwd 发送人密码

*/

public void setInitData(String host,String username,String passwd){

//创建邮件发送服务器

mailSender = new JavaMailSenderImpl();

mailSender.setHost(host);

// mailSender.setPort(465);

mailSender.setUsername(username);

mailSender.setPassword(passwd);

//加认证机制

Properties javaMailProperties = new Properties();

javaMailProperties.put("mail.smtp.auth", true);

javaMailProperties.put("mail.smtp.starttls.enable", true);

javaMailProperties.put("mail.smtp.timeout", 5000);

mailSender.setJavaMailProperties(javaMailProperties);

System.out.println("初始化邮件发送信息完成");

}

/**

* 发送普通文本

* @param email 对方邮箱地址

* @param subject 主题

* @param text 邮件内容

*/

public void simpleMailSend(String email,String subject,String text) {

//创建邮件内容

SimpleMailMessage message=new SimpleMailMessage();

message.setFrom(mailSender.getUsername());

message.setTo(email);

message.setSubject(subject);

message.setText(text);

//发送邮件

mailSender.send(message);

System.out.println("发送成功");

}

/**

* 发送附件,支持多附件

* //使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容

//MimeMessages为复杂邮件模板,支持文本、附件、html、图片等。

* @param email 对方邮箱

* @param subject 主题

* @param text 内容

* @param paths 附件路径,和文件名

* @throws MessagingException

*/

public void attachedSend(String email,String subject,String text,Map paths) throws MessagingException {

MimeMessage message = mailSender.creahttp://teMimeMessage();

//创建MimeMessageHelper对象,处理MimeMessage的辅助类

MimeMessageHelper helper = new MimeMessageHelper(message, true);

//使用辅助类MimeMessage设定参数

helper.setFrom(mailSender.getUsername());

helper.setTo(email);

helper.setSubject(subject);

helper.setText(text);

if (paths!=null){

paths.forEach((k,v)->{

//加载文件资源,作为附件

FileSystemResource file = new FileSystemResource(v);

try {

//添加附件

helper.addAttachment(k, file);

} catch (Exception e) {

e.printStackTrace();

}

});

}

//发送邮件

mailSender.send(message);

System.out.println("发送成功");

}

/**

* 发送html文件,支持多图片

* @param email 对方邮箱

* @param subject 主题

* @param text 内容

* @param paths 富文本中添加用到的路径,一般是图片,或者css,js文件

* @throws MessagingException

*/

public void richContentSend(String email,String subject,String text,Map paths) throws MessagingException {

MimeMessage message = mailSender.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(mailSender.getUsername());

helper.setTo(email);

helper.setSubject(subject);

//第二个参数true,表示text的内容为html,然后注意标签,src='cid:file','cid'是contentId的缩写,'file'是一个标记,

//需要在后面的代码中调用MimeMessageHelper的addInline方法替代成文件

helper.setText(text,true);

//文件地址相对应src目录

// ClassPathResource file = new ClassPathResource("logo.png");

if (paths!=null){

paths.forEach((k,v)->{

//文件地址对应系统目录

FileSystemResource file = new FileSystemResource(v);

try {

helper.addInline(k, file);

} catch (Exception e) {

e.printStackTrace();

}

});

}

mailSender.send(message);

System.out.println("发送成功");

}

/**

* 群发多人,且多附件

* @param mailto 多人邮件地址

* @param subject 主题

* @param text 内容

* @param filePath 文件路径

* @throws Exception

*/

public void sendBatchMailWithFile(String[] emails, String subject, String text, String[] filePath) throws Exception {

MimeMessage mimeMessage = mailSender.createMimeMessage();

MimeMessahttp://geHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

messageHelper.setFrom(new InternetAddress(MimeUtility.encodeText(mailSender.getUsername())));

messageHelper.setSubject(subject);

if (filePath != null) {

BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象

mdp.setContent(text, "text/html;charset=UTF-8");// 给BodyPart对象设置内容和格式/编码方式

Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象

mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)

// 把mm作为消息对象的内容

MimeBodyPart filePart;

FileDataSource filedatasource;

// 逐个加入附件

for (int j = 0; j < filePath.length; j++) {

filePart = new MimeBodyPart();

filedatasource = new FileDataSource(filePath[j]);

filePart.setDataHandler(new DataHandler(filedatasource));

try {

filePart.setFileName(MimeUtility.encodeText(filedatasource.getName()));

} catch (Exception e) {

e.printStackTrace();

}

mm.addBodyPart(filePart);

}

mimeMessage.setContent(mm);

} else {

messageHelper.setText(text, true);

}

List list = new ArrayList();// 不能使用string类型的类型,这样只能发送一个收件人

for (int i = 0; i < emails.length; i++) {

list.add(new InternetAddress(emails[i]));

}

InternetAddress[] address = list.toArray(new InternetAddress[list.size()]);

mimeMessage.setRecipients(Message.RecipientType.TO, address);

mimeMessage = messageHelper.getMimeMessage();

mailSender.send(mimeMessage);

System.out.println("发送成功");

}

public static void main(String[] args) throws Exception

{

SendMailUtils test = new SendMailUtils();

//测试发送普通文本

// test.setInitData("smtp.qq.com","706548532@qq.com","123456");

test.setInitData("smtp.163.com","1234@163.com","1234");

test.simpleMailSend("1326624701@qq.com","测试","测试能不能发邮件!!!");

//测试发送附件

/* test.setInitData("smtp.163.com","1234@163.com","1234");

Map map = new HashMap();

map.put("test12.xls", "D:\\tomcat8\\apache-tomcat-8.0.29\\test12.xls");

map.put("wsdl.rar", "D:\\wsdl.rar");

test.attachedSend("706548532@qq.com","Hello Attachment","This is a mail with attachment",map);

*/

//测试发送富文本(html文件)

/* test.setInitData("smtp.163.com","1234@163.com","1234");

String text = "

Hello Html Email

Map map = new HashMap();

map.put("file", "E:\\1f7827.jpg");

test.richContentSend("706548532@qq.com","邮件标题",text,map);*/

//测试群发多人多附件

test.setInitData("smtp.163.com","1234@163.com","1234");

String [] address = {"706548532@qq.com","1326624701@qq.com"};

String [] filePath = {"D:\\tomcat8\\apache-tomcat-8.0.29\\test12.xls", "D:\\wsdl.rar"};

test.sendBatchMailWithFile(address, "群发多文件", "实时", filePath);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口自动化框架 大数据库(接口自动化框架 大数据库是什么)
下一篇:自动接口测试框架(自动测试及接口技术)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~