话不多说,直接上代码!!!
/*
新建传输对象类
*/
import lombok.*;
import java.util.List;
import java.util.Map;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
public class SapSendMessageVO {
// 发件人的邮箱的服务器地址
private String emailSMTPHost;
// 使用的协议
private String protocol;
// 发送人邮箱
private String SenderEmail;
// 发件人姓名
private String SenderName;
// 发送人邮箱授权码
private String senderCode;
// 收件人邮箱
private List<Map<String,String>> receiveMailAccount;
// 抄送人邮箱
private List<Map<String,String>> ccMailAccount;
// 文件名称
private String fileName;
// 附件base64
private String file;
}
/*
创建邮件信息方法
*/
private static MimeMessage createMimeMessage(Session session,SapSendMessageVO sapSendMessageVO) throws Exception {
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人
message.setFrom(new InternetAddress(sapSendMessageVO.getSenderEmail(), sapSendMessageVO.getSenderName(), "UTF-8"));
// 3. MimeMessage.RecipientType.TO:收件类型;MimeMessage.RecipientType.CC:抄送类型;MimeMessage.RecipientType.BCC:密送类型
List<Map<String,String>> recList = sapSendMessageVO.getReceiveMailAccount();
List<Map<String,String>> ccList = sapSendMessageVO.getCcMailAccount();
if (recList.size() > 0) {
for (Map<String,String> map : recList) {
message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(map.get("recAddresses"), map.get("recName"), "UTF-8"));
}
} else {
throw new CommonException("邮件必须要有收件人!");
}
if (ccList.size() > 0) {
for (Map<String,String> map : ccList) {
message.addRecipient(MimeMessage.RecipientType.CC, new InternetAddress(map.get("ccAddresses"), map.get("ccName"), "UTF-8"));
}
}
// 4. Subject: 邮件主题
message.setSubject("这是邮件的主题", "UTF-8");
// 5. Content: 邮件正文(可以使用html标签)
message.setContent("这是邮件正文", "text/html;charset=UTF-8");
// 6. 附件
// 将base64格式的文件解码,写入指定地址
String filePath = "/home/jiantou/tkjt/one-contract/sapEmail/" + File.separator + sapSendMessageVO.getFileName();
// String filePath = "D:\\file\\"+sapSendMessageVO.getFileName();
try {
byte[] buffer = new BASE64Decoder().decodeBuffer(sapSendMessageVO.getFile());
for(int i =0 ; i < buffer.length ;i++) {
if(buffer[i] < 0) {
buffer[i] += 256;
}
}
FileOutputStream out = new FileOutputStream(filePath);
out.write(buffer);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart file1 = new MimeBodyPart();
DataHandler handler = new DataHandler(new FileDataSource(filePath));
file1.setDataHandler(handler);
//对文件名进行编码,防止出现乱码
String fileName = MimeUtility.encodeWord(sapSendMessageVO.getFileName(), "utf-8", "B");
file1.setFileName(fileName);
multipart.addBodyPart(file1);
message.setContent(multipart);
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
return message;
}
/*
发送邮件方法
*/
public void sapSendMessage(SapSendMessageVO sapSendMessageVO) {
log.info("=========== sap调用接口,开始发送邮件 =========");
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", sapSendMessageVO.getProtocol());// 使用的协议
props.setProperty("mail.smtp.host", sapSendMessageVO.getEmailSMTPHost());// 发件人的邮箱的SMTP服务器地址
props.setProperty("mail.smtp.auth", "true");// 需要请求认证
Session session = Session.getInstance(props);//得到会话对象实例
session.setDebug(true);//是否打印详细日志
MimeMessage message = createMimeMessage(session,sapSendMessageVO);//获取邮件对象(封装了一个方法)
Transport transport = session.getTransport();
transport.connect(sapSendMessageVO.getSenderEmail(), sapSendMessageVO.getSenderCode());//连接发送人的邮箱账户
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人
log.info("======== 发送邮件地址数量为: ======="+message.getAllRecipients().length);
log.info("======== 发送邮件地址为: ======="+message.getAllRecipients().toString());
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
transport.close();
log.info("邮件发送成功");
} catch (Exception e) {
log.error("发送邮件失败");
}
}