邮件推送:
需要在阿里云上面根据你的域名,申请一个阿里邮箱地址。然后在代码中配置相关信息即可发送,这个官方有文档,地址:点击飞过去,
坑爹项:发送的记录没有地方可以查询,只能通过自己系统去记录。
企业邮箱:通过自己的阿里云账号,密码,发送邮件
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- org.apache.commons.lang3.StringUtils-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
了解一下基本的配置元素概念:
SMTP的全称是"Simple Mail Transfer Protocol", 即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范, 通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP协议簇, 它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP服务器就是遵循SMTP协议发送邮件的服务器。
SMTP 认证, 简单说就是要求必须在提供账户名和密码之后才可以登陆SMTP服务器, 这就使得那些垃圾邮件的散播者无可乘之机。增加SMTP认证的目的是为了使用户避免收到垃圾邮件的干扰。
具体代码如下:
package com.coding.mail.controller;
import org.apache.commons.lang3.StringUtils;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.Security;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
//import org.apache.commons.lang3.StringUtils;
public class AliyunMail {
// private static final String ALIDM_SMTP_HOST = "smtp.qiye.aliyun.com";
private static final String ALIDM_SMTP_HOST = "smtp.aliyun.com";
private static final String ALIDM_SMTP_PORT = "465";
public static void main(String[] args) {
try {
sendMail("1945739215@aliyun.com", "sshhmm0908.", ALIDM_SMTP_HOST, ALIDM_SMTP_PORT, "subject test",
"<H1>测试</H1>", "1945739215@qq.com", "E:/A_python/EQIL_Database_2022.csv", null);
} catch (Exception e) {
System.out.println("出现异常");
}
}
/**
* @param sendAddress 发件人地址
* @param sendPassword 发件人密码
* @param host 协议 smtp.qiye.aliyun.com
* @param port 端口 465
* @param subject 标题
* @param content 内容
* @param filePath 附件地址 D:/题库上传模板v1.xlsx
* @param CC 抄送人
* @throws Exception
* @throws AddressException
*/
public static void sendMail(String sendAddress,String sendPassword,String host,String port,String subject,String content,String internetAddress,String filePath,String CC) throws AddressException, Exception {
//设置SSL连接、邮件环境
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", port);//设置端口
props.setProperty("mail.debug", "true");//启用调试
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.auth", "true");
//建立邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator() { //身份认证
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(sendAddress, sendPassword);//发件人账号、密码
}
});
//建立邮件对象
MimeMessage message = new MimeMessage(session);
//设置邮件的发件人、收件人、主题
//附带发件人名字
// message.setFrom(new InternetAddress("from_mail@qq.com", "optional-personal"));
message.setFrom(new InternetAddress(sendAddress));//发件人账号
message.setRecipients(Message.RecipientType.TO, internetAddress);//收件人账号
//标题
message.setSubject(subject);//邮件标题
//内容
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(content, "text/html;charset=utf-8");//邮件内容
multipart.addBodyPart(contentPart);
//附件部分
if (StringUtils.isNotBlank(filePath)) {
BodyPart attachPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filePath); //附件地址
attachPart.setDataHandler(new DataHandler(fileDataSource));
attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
multipart.addBodyPart(attachPart);
}
message.setContent(multipart);
//抄送地址
if (StringUtils.isNotBlank(CC)) {
InternetAddress[] internetAddressCC = new InternetAddress().parse(CC);
message.setRecipients(Message.RecipientType.CC, internetAddressCC);
}
//发送邮件
Transport.send(message);
}
}