這是我的目錄
- 一、開啟郵箱服務
- 撰寫發送郵件程式
今天公司某一個業務需要給顧客發郵件進行營銷,并且需要附帶附件,公司當前對該功能還沒有進行封裝,我暫時對于使用Java程式發送郵件練習一個小demo,
本文涉及到的知識點:
1.如何使用Java程式發送郵件
2.如何發送有附件的郵件
3.如何進行群發郵件
4.如何攜帶多個附件
一、開啟郵箱服務
- 打開QQ郵箱客戶端,點擊設定

- 打開服務,并獲取授權碼(注意授權碼很重要,為了保證傳輸的安全,我們后續使用傳輸中需要使用授權碼作為密碼的校驗)
- 注意這一步可能需要我們使用系結郵箱的手機號發送短信,然后回傳頁面查看授權碼就OK了

撰寫發送郵件程式
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
import com.alibaba.druid.util.StringUtils;
import com.sun.mail.util.MailSSLSocketFactory;
/**
* 發送郵件工具類
*
* @author leiyu
*/
public class SendMailAcceUtils {
/**
* 發送帶附件的郵件
*
* @param receive 收件人
* @param subject 郵件主題
* @param msg 郵件內容
* @param filename 附件地址
* @return
* @throws GeneralSecurityException
*/
public static boolean sendMail(String receive, String subject, String msg, String filename)
throws GeneralSecurityException {
if (StringUtils.isEmpty(receive)) {
return false;
}
// 發件人電子郵箱
final String from = "1010866409@qq.com";
// 發件人電子郵箱密碼
final String pass = "xxxxxxxxxx";//注意這部分就是你獲取的授權碼
// 指定發送郵件的主機為 smtp.qq.com
String host = "smtp.qq.com"; // 郵件服務器
// 獲取系統屬性
Properties properties = System.getProperties();
// 設定郵件服務器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 獲取默認session物件
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() { // qq郵箱服務器賬戶、第三方登錄授權碼
return new PasswordAuthentication(from, pass); // 發件人郵件用戶名、密碼
}
});
try {
// 創建默認的 MimeMessage 物件
MimeMessage message = new MimeMessage(session);
// Set From: 頭部頭欄位
message.setFrom(new InternetAddress(from));
// Set To: 頭部頭欄位
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
// Set Subject: 主題文字
message.setSubject(subject);
// 創建訊息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 訊息
messageBodyPart.setText(msg);
// 創建多重訊息
Multipart multipart = new MimeMultipart();
// 設定文本訊息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
// 設定要發送附件的檔案路徑
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName(filename);
// 處理附件名稱中文(附帶檔案路徑)亂碼問題
messageBodyPart.setFileName(MimeUtility.encodeText(filename));
multipart.addBodyPart(messageBodyPart);
// 發送完整訊息
message.setContent(multipart);
// 發送訊息
Transport.send(message);
// System.out.println("Sent message successfully....");
return true;
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
String receive="1010866409@qq.com";
String subject ="郵件主題";
String msg ="郵件內容";
String filename ="E:\\a.excel";
try {
SendMailAcceUtils.sendMail(receive, subject, msg, filename);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
}
發送郵件— 實作群發
實作郵件的群發,實際上就是發件人的郵箱是多個,我們可能有多種方式進行郵件的發送,這里舉例是使用最簡單遍歷的方式實作郵件的群發,
public static void main(String[] args) {
/******實作Java郵件的群發*****/
ArrayList<String> strings = new ArrayList<String>();
strings.add("1010866409@qq.com");
strings.add("1010866409@qq.com");
/*****************/
// String receive="1010866409@qq.com";
for (int i = 0; i < strings.size() ; i++) {
String receive=strings.get(i);
String subject ="這是一個郵件主題(歡迎您選擇我們得營銷策略)";
String msg ="郵件內容(歡迎來店選購適合您的產品)";
String filename ="E:\\a.md";
try {
SendMailAcceUtils.sendMail(receive, subject, msg, filename);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/289570.html
標籤:java
