一,工具类
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
/**
* 邮件发送操作类
*
* @author tst
*
*/
public class MailOperation {
/**
* 发送邮件
* @param user 发件人邮箱
* @return success 发送成功 failure 发送失败
* @throws Exception
*/
public String sendMail(MailSenderInfo mailSenderInfo)
throws Exception {
if (mailSenderInfo.getTo() != null){
Properties props = System.getProperties();
props.put("mail.smtp.host", mailSenderInfo.getHost());
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator();
MailAuthenticator.USERNAME = mailSenderInfo.getUser();
MailAuthenticator.PASSWORD = mailSenderInfo.getPassword();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailSenderInfo.getFrom()));
if (!mailSenderInfo.getTo().trim().equals(""))
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(mailSenderInfo.getTo().trim()));
message.setSubject(mailSenderInfo.getSubject());
MimeBodyPart mbp1 = new MimeBodyPart(); // 正文
mbp1.setContent(mailSenderInfo.getContent(), "text/html;charset=utf-8");
Multipart mp = new MimeMultipart(); // 整个邮件:正文+附件
// 为邮件添加附件
String[] attachFileNames = mailSenderInfo.getAnnexs();
if (attachFileNames != null && attachFileNames.length > 0) {
// 存放邮件附件的MimeBodyPart
MimeBodyPart attachment = null;
File file = null;
for (int i = 0; i < attachFileNames.length; i++) {
attachment = new MimeBodyPart();
// 根据附件文件创建文件数据源
file = new File(attachFileNames[i]);
FileDataSource fds = new FileDataSource(file);
attachment.setDataHandler(new DataHandler(fds));
// 为附件设置文件名
attachment.setFileName(MimeUtility.encodeWord(
file.getName(), "GBK", null));
mp.addBodyPart(attachment);
}
}
mp.addBodyPart(mbp1);
message.setContent(mp);
message.setSentDate(new Date());
message.saveChanges();
Transport trans = session.getTransport("smtp");
trans.send(message);
System.out.println(message.toString());
} catch (Exception e){
e.printStackTrace();
return "failure";
}
return "success";
}else{
return "failure";
}
}
}
二,认证配置类
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* 发件人账号密码
* @author tst
*
*/
public class MailAuthenticator extends Authenticator{
public static String USERNAME = "";
public static String PASSWORD = "";
public MailAuthenticator() {
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
}
三,账户配置类
/**
* @ClassName MailSenderInfo
* @Description TODO
* @Author tst
* @Date 2019/9/2317:14
* @Version
**/
public class MailSenderInfo {
private String user;//发件人邮箱
private String password;//授权码(注意不是邮箱登录密码)
private String host;
private String from;//发件人
private String to;//接收者邮箱
private String subject;//邮件主题
private String content;//邮件内容
private String[] annexs;//邮件附件
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String[] getAnnexs() {
return annexs;
}
public void setAnnexs(String[] annexs) {
this.annexs = annexs;
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于