企业微信邮箱绑定微信
首先绑定微信,在安全设置处开启安全登录
腾讯企业邮箱开启SMTP 服务
在收发信设置选项中开启SMTP 服务
生成企业微信邮箱(通常是通过腾讯企业邮箱 Exmail 提供的)所需的授权码
回到邮箱绑定选中生成授权码(记得复制保存不然就得重复停用开启!!!)
拿到授权码后开始代码
我们需要引入一个Jar包
这是一个为方便Java 开发人员在应用程序中实现邮件发送和接收功能而提供的一套标准开发包,它支持一些常用的邮件协议 比如上面将的SMTP ,POP3 ,使用 Apache POI 生成 Excel 文件等。
<!-- Apache POI 用于生成 Excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- Java Mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
发送邮件工具类(支持附件)
注意替换发送者邮箱地址及发送者授权码
import org.apache.commons.collections.CollectionUtils;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.List;
import java.util.Properties;
public class SendMailUtil {
/**
* 发件服务器地址(以下是腾讯企业邮箱)
*/
private static String MAIL_SMTP_HOST = "smtp.exmail.qq.com";
/**
* 端口
*/
private static String MAIL_SMTP_PORT = "465";
/**
* 使用smtp身份验证
*/
private static String MAIL_SMTP_AUTH = "true";
/**
* 使用SSL,企业邮箱必需
*/
private static String MAIL_SMTP_SSL_ENABLE = "true";
/**
* 发送者邮箱地址
*/
private static String MAIL_FROM_EMAIL = "yanxiaosheng";
/**
* 发送者授权码
*/
private static String MAIL_PASS_WORD = "xxxxxxxxxxx";
/**
* 邮箱配置
* @param toEmail 接收人邮箱地址
* @param subject 邮件主题
* @param body 邮件内容
* @param attachmentPaths 附件(可为空)
*
*/
public Boolean setTencentExEmail (String toEmail, String subject, String body, List<String> attachmentPaths){
Properties prop = new Properties();
//服务器
prop.setProperty("mail.smtp.host", MAIL_SMTP_HOST);
//端口
prop.setProperty("mail.smtp.port", MAIL_SMTP_PORT);
//使用smtp身份验证
prop.setProperty("mail.smtp.auth", MAIL_SMTP_AUTH);
//使用smtp身份验证
prop.setProperty("mail.smtp.ssl.enable", MAIL_SMTP_SSL_ENABLE);
try {
Session session = Session.getInstance(prop, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(MAIL_FROM_EMAIL, MAIL_PASS_WORD);
}
});
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(MAIL_FROM_EMAIL));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
msg.setSubject(subject);
// 正文 + 附件
Multipart multipart = new MimeMultipart();
// 添加正文
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(body);
multipart.addBodyPart(textPart);
if (!CollectionUtils.isEmpty(attachmentPaths)) {
// 添加多个附件
for (String path : attachmentPaths) {
MimeBodyPart attachment = new MimeBodyPart();
attachment.attachFile(new File(path));
multipart.addBodyPart(attachment);
}
}
msg.setContent(multipart);
Transport.send(msg);
return true;
}catch (Exception e){
return false;
}
}
}
如需添加Excel附件
集合转为excel传入工具类
private String getAlarmTypeExcel(List<JSONObject> alarmTypeList ,String excelFilePath){
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("报警名称数据");
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("报警名称");
header.createCell(1).setCellValue("报警次数");
int rowIndex = 1;
for (JSONObject alarmType : alarmTypeList) {
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(alarmType.getString(""));
row.createCell(1).setCellValue(alarmType.getString(""));
}
// 自动列宽
for (int i = 0; i < 2; i++) {
sheet.autoSizeColumn(i);
}
try {
// 写入文件
FileOutputStream out = new FileOutputStream(excelFilePath);
workbook.write(out);
out.close();
workbook.close();
}catch (Exception e){
e.printStackTrace();
}
return excelFilePath;
}
发送邮件
SendMailUtil.setTencentExEmail(
接收者邮箱地址,
邮件主题,
邮件内容,
Arrays.asList(getAlarmTypeExcel(alarmTypeList,"报警名称数据.xlsx")));