目錄
- 推薦公衆号
- 正文
-
- qq郵箱開通smtp服務
- 代碼
- 郵箱檔案名亂碼問題
推薦公衆号
有彩蛋哦!!!(或者公衆号内點選網賺擷取彩蛋)
正文
qq郵箱開通smtp服務
代碼
郵箱檔案名亂碼問題
要和浏覽器網頁編碼一緻,由于更新過谷歌浏覽器是以看不到網頁編碼;
通過火狐浏覽器看到預設網頁編碼是簡體中文,也就是GB2312
emailContent 裡面包含
郵件标題 和 正文
emailConfig 包含
host smtp.qq.co
FromUser 誰發送的
token 授權碼
public class SendMailUtils {
//host = smtp.qq.com fromUser = [email protected] token = olcsomniofpaibdh
public static void sendEmail(EmailContent emailContent, EmailConfig emailConfig, Map<String, String> fileInfos) throws Exception {
Properties prop = new Properties();
// 開啟debug調試,以便在控制台檢視
prop.setProperty("mail.debug", "true");
// 設定郵件伺服器主機名
prop.setProperty("mail.host", emailConfig.getHost());
// 發送伺服器需要身份驗證
prop.setProperty("mail.smtp.auth", "true");
// 發送郵件協定名稱
prop.setProperty("mail.transport.protocol", "smtp");
// 開啟SSL加密,否則會失敗
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 建立session
Session session = Session.getInstance(prop);
// 通過session得到transport對象
Transport ts = session.getTransport();
// 連接配接郵件伺服器:郵箱類型,帳号,授權碼
ts.connect(emailConfig.getHost(), emailConfig.getFromUser(), emailConfig.getToken());
// 建立郵件
Message message = createSimpleMail(emailContent, session, fileInfos);
// 發送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createSimpleMail
* @Description: 建立一封隻包含文本的郵件
*/
public static MimeMessage createSimpleMail(EmailContent emailContent, Session session, Map<String, String> fileInfos)
throws Exception {
// 建立郵件對象
MimeMessage message = new MimeMessage(session);
// 指明郵件的發件人
message.setFrom(new InternetAddress("[email protected]"));
// 指明郵件的收件人,現在發件人和收件人是一樣的,那就是自己給自己發
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
// 郵件的标題
message.setSubject(emailContent.getTitle());
// 郵件的文本内容
message.setContent(emailContent.getContent(), "text/html;charset=UTF-8");
// 向multipart對象中添加郵件的各個部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 設定郵件的文本内容
// BodyPart contentPart = new MimeBodyPart();
// contentPart.setText("第二種方法···");
// multipart.addBodyPart(contentPart);
addFiles(fileInfos, multipart);
// 将multipart對象放到message中
message.setContent(multipart);
// 傳回建立好的郵件對象
return message;
}
private static void addFiles(Map<String, String> fileInfos, Multipart multipart) throws MessagingException {
if (MapUtils.isEmpty(fileInfos)) {
return;
}
// 添加附件
for (Map.Entry<String, String> entry : fileInfos.entrySet()) {
String fileName = entry.getKey();
String filePath = entry.getValue();
BodyPart bodyPart = getBodyPart(filePath, fileName);
multipart.addBodyPart(bodyPart);
}
}
public static BodyPart getBodyPart(String filePath, String fileName) throws MessagingException {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filePath);
// 添加附件的内容
messageBodyPart.setDataHandler(new DataHandler(source));
// 添加附件的标題
// 這裡很重要,通過下面的Base64編碼的轉換可以保證你的中文附件标題名在發送時不會變成亂碼
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
messageBodyPart.setFileName("=?GB2312?B?"
+ enc.encode(fileName.getBytes()) + "?=");
return messageBodyPart;
}
}