這是個人項目中使用的郵箱發送的,基於ssm使用,關於springboot繼承的請自行百度。
package com.o2o.common.mail;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* 郵箱推送工具類
* @author Administrator
*
*/
public class MailUtil {
private static final MailUtil instance = new MailUtil(); /// 直接初始化一個實例對象
private MailUtil() {
}
public static MailUtil getInstance() {
return instance;
}
//域名服務器
private static final String host = "";
//用戶名
private static final String account = "";
//授權碼
private static final String authorizationCode = "";
//收件人郵箱
private static final String from = "";
/**
* 郵件發送靜態方法
* @param host smtp服務器地址,,示例 smtp.163.com
* @param account 郵箱用戶名
* @param authorizationCode 授權碼
*/
public void Sendmail(String to,String subject,String content) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", host);
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
// 使用JavaMail發送郵件的5個步驟
// 1、創建session
Session session = Session.getInstance(prop);
// 開啓Session的debug模式,這樣就可以查看到程序發送Email的運行狀態
session.setDebug(true);
// 2、通過session得到transport對象
Transport ts = session.getTransport();
// 3、連上郵件服務器,需要發件人提供郵箱的用戶名和密碼進行驗證
ts.connect(host, account, authorizationCode);
// 4、創建郵件
//Message message = createSimpleMail(session, to, subject, content);
Message message = sendHtmlMail(session, to, subject, content);
// 5、發送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createSimpleMail
* @Description: 創建一封只包含文本的郵件
* @Anthor:劉偉剛
* @param session
* @param from 發件人郵箱地址
* @param to 收件人郵箱地址
* @param subject 郵件主題
* @param content 郵件文檔內容,
* @return
* @throws Exception
*/
private MimeMessage createSimpleMail(Session session,String to,String subject,String content) throws Exception {
// 創建郵件對象
MimeMessage message = new MimeMessage(session);
// 指明郵件的發件人
message.setFrom(new InternetAddress(from));
// 指明郵件的收件人,現在發件人和收件人是一樣的,那就是自己給自己發
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 郵件的標題
message.setSubject(subject);
// 郵件的文本內容
message.setContent(content, "text/html;charset=UTF-8");
// 返回創建好的郵件對象
return message;
}
private MimeMessage sendHtmlMail(Session session,String to,String subject,String content)throws Exception{
// 創建郵件對象
MimeMessage message = new MimeMessage(session);
// 指明郵件的發件人
message.setFrom(new InternetAddress(from));
// 指明郵件的收件人,現在發件人和收件人是一樣的,那就是自己給自己發
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 消息發送的時間
message.setSentDate(new Date());
// 郵件的標題
message.setSubject(subject);
// MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象
Multipart mainPart = new MimeMultipart();
// 創建一個包含HTML內容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 設置HTML內容
html.setContent(content, "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 將MiniMultipart對象設置爲郵件內容
message.setContent(mainPart);
return message;
}
/**
* @Method: createImageMail
* @Description: 生成一封郵件正文帶圖片的郵件
* @Anthor:劉偉剛
* @param session
* @return
* @throws Exception
*/
private MimeMessage createImageMail(Session session,String from,String to,String subject,String content,String imgpath) throws Exception {
//創建郵件
MimeMessage message = new MimeMessage(session);
// 設置郵件的基本信息
//發件人
message.setFrom(new InternetAddress(from));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
//郵件標題
message.setSubject(subject);
// 準備郵件數據
// 準備郵件正文數據
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8"); //"這是一封郵件正文帶圖片<img src='cid:xxx.jpg'>的郵件"
// 準備圖片數據
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(imgpath)); //"src\\1.jpg"
image.setDataHandler(dh);
image.setContentID(UUID.randomUUID()+".jpg");
// 描述數據關係
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
message.setContent(mm);
message.saveChanges();
//將創建好的郵件寫入到E盤以文件的形式進行保存
message.writeTo(new FileOutputStream("E:\\ImageMail.eml"));
//返回創建好的郵件
return message;
}
/**
* @Method: createAttachMail
* @Description: 創建一封帶附件的郵件
* @Anthor:劉偉剛
*
* @param session
* @return
* @throws Exception
*/
private MimeMessage createAttachMail(Session session) throws Exception {
MimeMessage message = new MimeMessage(session);
// 設置郵件的基本信息
// 發件人
message.setFrom(new InternetAddress("[email protected]"));
// 收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
// 郵件標題
message.setSubject("JavaMail郵件發送測試");
// 創建郵件正文,爲了避免郵件正文中文亂碼問題,需要使用charset=UTF-8指明字符編碼
MimeBodyPart text = new MimeBodyPart();
text.setContent("使用JavaMail創建的帶附件的郵件", "text/html;charset=UTF-8");
// 創建郵件附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\2.jpg"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); //
// 創建容器描述數據關係
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
// 將創建的Email寫入到E盤存儲
message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
// 返回生成的郵件
return message;
}
/**
* @Method: createMixedMail
* @Description: 生成一封帶附件和帶圖片的郵件
* @Anthor:劉偉剛
* @param session
* @return
* @throws Exception
*/
private MimeMessage createMixedMail(Session session) throws Exception {
// 創建郵件
MimeMessage message = new MimeMessage(session);
// 設置郵件的基本信息
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("帶附件和帶圖片的的郵件");
// 正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("xxx這是女的xxxx<br/><img src='cid:aaa.jpg'>", "text/html;charset=UTF-8");
// 圖片
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src\\3.jpg")));
image.setContentID("aaa.jpg");
// 附件1
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\4.zip"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
// 附件2
MimeBodyPart attach2 = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip"));
attach2.setDataHandler(dh2);
attach2.setFileName(MimeUtility.encodeText(dh2.getName()));
// 描述關係:正文和圖片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(image);
mp1.setSubType("related");
// 描述關係:正文和附件
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attach);
mp2.addBodyPart(attach2);
// 代表正文的bodypart
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
mp2.setSubType("mixed");
message.setContent(mp2);
message.saveChanges();
message.writeTo(new FileOutputStream("E:\\MixedMail.eml"));
// 返回創建好的的郵件
return message;
}
}