Java實現郵箱傳送功能例項(阿里雲郵箱推送)
阿新 • • 發佈:2020-09-22
前言
繫結阿里雲域名,建立域名賬戶,並配置解析域名賬戶,獲得傳送郵箱的許可權,上限兩百封,超出要¥…
阿里雲郵件推送控制檯
https://www.aliyun.com/product/directmail
申請郵箱並解析域名
https://yq.aliyun.com/articles/707997
1. 郵箱資訊實體類
package cn.bitqian.mail.test; /** * 郵件內容實體類 * @author echo lovely * @date 2020/9/18 15:47 */ public class MailInfo { // 發件人賬戶 private String sendEmailAccount; // 發件人密碼 private String sendEmailPassword; // 收件人賬戶 private String receiveMailAccount; // 傳送人姓名 private String sendPersonName; // 收件人姓名 private String receivePersonName; // 郵件標題 private String mailTitle; // 郵件正文 private String mailContent; public MailInfo(String sendEmailAccount,String sendEmailPassword,String receiveMailAccount,String sendPersonName,String receivePersonName,String mailTitle,String mailContent) { this.sendEmailAccount = sendEmailAccount; this.sendEmailPassword = sendEmailPassword; this.receiveMailAccount = receiveMailAccount; this.sendPersonName = sendPersonName; this.receivePersonName = receivePersonName; this.mailTitle = mailTitle; this.mailContent = mailContent; } public String getSendEmailAccount() { return sendEmailAccount; } public void setSendEmailAccount(String sendEmailAccount) { this.sendEmailAccount = sendEmailAccount; } public String getSendEmailPassword() { return sendEmailPassword; } public void setSendEmailPassword(String sendEmailPassword) { this.sendEmailPassword = sendEmailPassword; } public String getReceiveMailAccount() { return receiveMailAccount; } public void setReceiveMailAccount(String receiveMailAccount) { this.receiveMailAccount = receiveMailAccount; } public String getMailTitle() { return mailTitle; } public void setMailTitle(String mailTitle) { this.mailTitle = mailTitle; } public String getMailContent() { return mailContent; } public void setMailContent(String mailContent) { this.mailContent = mailContent; } public String getSendPersonName() { return sendPersonName; } public void setSendPersonName(String sendPersonName) { this.sendPersonName = sendPersonName; } public String getReceivePersonName() { return receivePersonName; } public void setReceivePersonName(String receivePersonName) { this.receivePersonName = receivePersonName; } }
2. jar包依賴
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- mail dependency --> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency>
3. 傳送郵箱實現
注意:
1. 更改下面的屬性,與實體類對應
程式碼實現
package cn.bitqian.mail.test; import org.junit.Test; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties; /** * 郵件傳送工具類 * @author echo lovely * @date 2020/9/18 15:54 */ public class MailSendUtils { // 郵箱smtp協議 這裡是使用阿里雲的 public static final String myEmailSMTPHost = "smtpdm.aliyun.com"; @Test public void test() { MailInfo mailInfo = new MailInfo("控制檯所繫結的郵箱賬戶名","繫結賬戶郵箱密碼","收件人郵箱名","發件人姓名","收件人姓名","郵箱標題","郵箱內容"); try { sendEmail(mailInfo); } catch (Exception e) { e.printStackTrace(); } } public void sendEmail(MailInfo mailInfo) throws Exception { // 1. 建立引數配置,用於連線郵件伺服器的引數配置 Properties props = new Properties(); // 引數配置 props.setProperty("mail.transport.protocol","smtp"); // 使用的協議(JavaMail規範要求) props.setProperty("mail.smtp.host",myEmailSMTPHost); // 發件人的郵箱的 SMTP 伺服器地址 props.setProperty("mail.smtp.auth","true"); // 需要請求認證 // 2. 根據配置建立會話物件,用於和郵件伺服器互動 Session session = Session.getInstance(props); session.setDebug(true); // 設定為debug模式,可以檢視詳細的傳送 log // 3. 建立一封郵件 MimeMessage message = new MailSendUtils().createMimeMessage(session,mailInfo); // 4. 根據 Session 獲取郵件傳輸物件 Transport transport = session.getTransport(); // 5. 使用 郵箱賬號 和 密碼 連線郵件伺服器,這裡認證的郵箱必須與 message 中的發件人郵箱一致,否則報錯 transport.connect(mailInfo.getSendEmailAccount(),mailInfo.getSendEmailPassword()); // 6. 傳送郵件,發到所有的收件地址,message.getAllRecipients() 獲取到的是在建立郵件物件時新增的所有收件人,抄送人,密送人 transport.sendMessage(message,message.getAllRecipients()); // 7. 關閉連線 transport.close(); } public MimeMessage createMimeMessage(Session session,MailInfo mailInfo) throws Exception { // 1. 建立一封郵件 MimeMessage message = new MimeMessage(session); // 2. From: 發件人(暱稱有廣告嫌疑,避免被郵件伺服器誤認為是濫發廣告以至返回失敗,請修改暱稱) message.setFrom(new InternetAddress(mailInfo.getSendEmailAccount(),mailInfo.getSendPersonName(),"UTF-8")); // 3. To: 收件人(可以增加多個收件人、抄送、密送) message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(mailInfo.getReceiveMailAccount(),mailInfo.getReceivePersonName(),"UTF-8")); // 4. Subject: 郵件主題(標題有廣告嫌疑,避免被郵件伺服器誤認為是濫發廣告以至返回失敗,請修改標題) message.setSubject(mailInfo.getMailTitle(),"UTF-8"); // 5. Content: 郵件正文(可以使用html標籤)(內容有廣告嫌疑,避免被郵件伺服器誤認為是濫發廣告以至返回失敗,請修改傳送內容) message.setContent(mailInfo.getMailContent(),"text/html;charset=UTF-8"); // 6. 設定發件時間 message.setSentDate(new Date()); // 7. 儲存設定 message.saveChanges(); return message; } }
總結
到此這篇關於Java實現郵箱傳送功能(阿里雲郵箱推送)的文章就介紹到這了,更多相關Java郵箱傳送(阿里雲郵箱推送)內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!