<dependency>下面是我写的工具类
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
/**邮件发送公共类
* @author chenhongjie
*/
public class MailUtil {
private String[] receiveMailAccount;
private String[] ccMailAccount;
private String text;//邮件内容
private String title;
public MailUtil(String[] receiveMailAccount,String[] ccMailAccount){
this.receiveMailAccount=receiveMailAccount;
this.ccMailAccount=ccMailAccount;
}
public MailUtil(String[] receiveMailAccount){
this.receiveMailAccount=receiveMailAccount;
}
public static String sendEmailAccount = PropertiesUtil.getValue("sendEmailAccount");
public static String sendEmailPassword = PropertiesUtil.getValue("sendEmailPassword");
public static String myEmailSMTPHost = PropertiesUtil.getValue("myEmailSMTPHost");
/**
*发送邮件
* @param title 邮件标题
* @param text 邮件内容
*/
public void sendMail(String title,String text){
this.title=title;
this.text=text;
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
try {
Properties props = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
Session session = Session.getDefaultInstance(props);
session.setDebug(false);// 设置为debug模式, 可以查看详细的发送 log
// 3. 创建一封邮件
MimeMessage message = null;
message= createMimeMessage(session);
// 4. 根据 Session 获取邮件传输对象
Transport transport = session.getTransport();
transport.connect(sendEmailAccount, sendEmailPassword);
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建一封只包含文本的简单邮件
*
* @param session 和服务器交互的会话
* @param sendMail 发件人邮箱
* @param receiveMail 收件人邮箱
* @return
* @throws Exception
*/
private MimeMessage createMimeMessage(Session session){
try {
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人
message.setFrom(new InternetAddress(sendEmailAccount, "Sinozo项目管理", "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
if(receiveMailAccount!=null){
for(String receive:receiveMailAccount){
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receive,receive, "UTF-8"));
}
}
// Cc: 抄送
if(ccMailAccount!=null){
for(String cc:ccMailAccount){
message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(cc,cc, "UTF-8"));
}
}
// 4. Subject: 邮件主题
message.setSubject(title, "UTF-8");
// 5. Content: 邮件正文(可以使用html标签)
message.setContent(text, "text/html;charset=UTF-8");
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
return message;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args){
//邮件功能使用方法
String receiveMailAccount[]={"chenhongjie@sinozo.com"};
//String ccMailAccount[]={"huangzhiyi@sinozo.com"};
//MailUtil mailUtil = new MailUtil(receiveMailAccount,ccMailAccount);
MailUtil mailUtil = new MailUtil(receiveMailAccount);
mailUtil.sendMail("测试类123","我是一个测试<br><a href='http://www.yyjava.com'>星创</a>");
}
}
其中PropertiesUtil是我自己写的一个读取配置文件的工具类,因为我把发送邮箱和发送邮箱的密码都写到配置文件里面去了
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
public static String getValue(String key){
Properties prop=new Properties();
InputStream in=new PropertiesUtil().getClass().getResourceAsStream("/sinozoPushAdminConfigDefault/env.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
return prop.getProperty(key);
}
public static void main(String[] args) {
System.out.println(getValue("cloudflareDownDomain"));
}
}
我的配置文件如下
sendEmailAccount=xxxxx@sinozo.com
sendEmailPassword=xxxxxxx
myEmailSMTPHost=smtp.xxxxx.com