- 邮件发送工具类
import org.springframework.core.io.support.PropertiesLoaderUtils;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Set;
public class MailUtil {
private static Properties properties = null;
static{
properties = new Properties();
try {
//properties文件要放到src目录下
properties = PropertiesLoaderUtils.loadAllProperties("mailConfig.properties");
//遍历取值
Set<Object> objects = properties.keySet();
for (Object object : objects) {
System.out.println(properties.getProperty((String) object));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 普通文本邮件发送
*
* @param to
* 邮件发送对象
* @param content
* 邮件发送内容
* @param subject
* 邮件主题
* @throws MessagingException
* @throws AddressException
*/
public static void sendMail(String to, String content, String subject) throws AddressException, MessagingException {
// 1.创建连接对象,连接到邮箱服务器,以qq邮箱服务器为例,需要先开通qq的SMTP协议
Properties props = new Properties();
//邮件发送协议
props.put("mail.transport.protocol", properties.get("mail.transport.protocol"));
//邮件发送服务器
props.put("mail.smtp.host", properties.get("mail.smtp.host"));
//设置授权用户名和密码校验
props.put("mail.smtp.auth", properties.get("mail.smtp.auth"));
//邮件发送端口
props.put("mail.smtp.port", Integer.valueOf((String)properties.get("mail.smtp.port")));
//启用ssl加密方式发送
props.put("mail.smtp.ssl.enable", properties.get("mail.smtp.ssl.enable"));
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {// 认证信息
return new PasswordAuthentication((String) properties.get("serverMailAddress"), (String) properties.get("serverMailPwd"));
}
});
// 2.创建邮件对象
Message message = new MimeMessage(session);
// 发送人
message.setFrom(new InternetAddress((String) properties.get("serverMailAddress")));
// 接收人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 邮件主题
message.setSubject(subject);
// 邮件内容
Multipart multipart = new MimeMultipart("alternative");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent( content, "text/html; charset=utf-8" );
htmlPart.setHeader("Content-Type", "text/html");
multipart.addBodyPart( htmlPart );
message.setContent(multipart);
//message.setText(content);
// 3.发送邮件
Transport.send(message);
}
//带附件发送邮件
public static void sendMailWithAttach(String to, String content, String subject) throws AddressException, MessagingException {
// 1.创建连接对象,连接到邮箱服务器,以qq邮箱服务器为例,需要先开通qq的SMTP协议
Properties props = new Properties();
//邮件发送协议
props.put("mail.transport.protocol", properties.get("mail.transport.protocol"));
//邮件发送服务器
props.put("mail.smtp.host", properties.get("mail.smtp.host"));
//设置授权用户名和密码校验
props.put("mail.smtp.auth", properties.get("mail.smtp.auth"));
//邮件发送端口
props.put("mail.smtp.port", Integer.valueOf((String)properties.get("mail.smtp.port")));
//启用ssl加密方式发送
props.put("mail.smtp.ssl.enable", properties.get("mail.smtp.ssl.enable"));
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {// 认证信息
return new PasswordAuthentication((String) properties.get("serverMailAddress"), (String) properties.get("serverMailPwd"));
}
});
// 2.创建邮件对象
Message message = new MimeMessage(session);
// 发送人
message.setFrom(new InternetAddress((String) properties.get("serverMailAddress")));
// 接收人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 邮件主题
message.setSubject(subject);
// 创建一个MIME子类型为"mixed"的MimeMultipart对象,表示这是一封混合组合类型的邮件
MimeMultipart mailContent = new MimeMultipart("mixed");
message.setContent(mailContent);
// 邮件内容
//Multipart multipart = new MimeMultipart("alternative");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent( content, "text/html; charset=utf-8" );
htmlPart.setHeader("Content-Type", "text/html");
//multipart.addBodyPart( htmlPart );
//邮件附件
MimeBodyPart attach = new MimeBodyPart();
mailContent.addBodyPart(attach);
mailContent.addBodyPart(htmlPart);
String fileName = "E:\报表原sql\ll.sql";
FileDataSource source = new FileDataSource(fileName);
attach.setDataHandler(new DataHandler(source));
try {
attach.setFileName(MimeUtility.encodeText("SPR006.sql"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//message.setContent(multipart);
// 3.发送邮件
Transport.send(message);
}
public static void main(String[] args) {
String content = "我是用来测试邮件发送的。如果成功了就牛逼了,如果失败了说明真的很挫。";
String to = "jeffyang@xxxx.com";//这里需要改成接收邮件的真实邮箱地址
String subject = "邮件测试";
try {
//sendMail(to,content,subject);
sendMailWithAttach(to,content,subject);
System.out.println("邮件发送成功");
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("邮件发送失败");
}
}
2.存放于src目录下的邮件服务配置文件mailConfig.properties
这里以qq邮箱作为测试服务器
#邮件发送协议
mail.transport.protocol=smtp
#邮箱服务器
mail.smtp.host=smtp.qq.com
#mail.smtp.host=localhost
#是否设置授权用户名和密码校验
mail.smtp.auth=true
#邮件发送端口
mail.smtp.port=465
#mail.smtp.port=25
#是否启用ssl加密方式发送
mail.smtp.ssl.enable=true
#邮件发送人账号(这里需要改成自己的真实的qq账号)
serverMailAddress=XXXXX@qq.com
#密码
serverMailPwd=xxzcrpfgovdmbdee