先了解一下基本的配置元素的概念:
什么是POP3、SMTP?
1、什么是POP3:
POP3是Post Office Protocol3的簡(jiǎn)稱(chēng),即郵局協(xié)議的第3個(gè)版本,它規(guī)定怎樣將個(gè)人計(jì)算機(jī)連接到Internet的郵件服務(wù)器和下載電子郵件的電子協(xié)議。它是因特網(wǎng)電子郵件的第一個(gè)離線(xiàn)協(xié)議標(biāo)準(zhǔn),POP3允許用戶(hù)從服務(wù)器上把郵件存儲(chǔ)到本地主機(jī)(即自己的計(jì)算機(jī))上,同時(shí)刪除保存在郵件服務(wù)器上的郵件,而POP3服務(wù)器則是遵循 POP3協(xié)議的接收郵件服務(wù)器,用來(lái)接收電子郵件的。
2、什么是SMTP:
SMTP 的全稱(chēng)是“Simple MailTransfer Protocol”,即簡(jiǎn)單郵件傳輸協(xié)議。它是一組用于從源地址到目的地址傳輸郵件的規(guī)范,通過(guò)它來(lái)控制郵件的中轉(zhuǎn)方式。SMTP 協(xié)議屬于 TCP/IP 協(xié)議簇,它幫助每臺(tái)計(jì)算機(jī)在發(fā)送或中轉(zhuǎn)信件時(shí)找到下一個(gè)目的地。SMTP 服務(wù)器就是遵循 SMTP 協(xié)議的發(fā)送郵件服務(wù)器。
(SMTP 認(rèn)證,簡(jiǎn)單地說(shuō)就是要求必須在提供了賬戶(hù)名和密碼之后才可以登錄 SMTP 服務(wù)器,這就使得那些垃圾郵件的散播者無(wú)可乘之機(jī)。增加 SMTP 認(rèn)證的目的是為了使用戶(hù)避免受到垃圾郵件的侵?jǐn)_。)
代碼實(shí)現(xiàn)流程:1,引入的包:
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
2,郵箱信息在Properties中的相關(guān)配置:
smtpServer=smtp.aliyun.com port=465 fromUserName=你的阿里郵箱賬號(hào) fromUserPassword=你的郵箱密碼
這里注意了,很容易出錯(cuò),網(wǎng)上一搜基本的教程都是這樣的:smtpServer=smtp.aliyun.com
也就是properties.put("mail.smtp.host", smtp.aliyun.com);
但如果你使用的是阿里的企業(yè)郵箱,這樣把郵箱的配置服務(wù)器地址照搬過(guò)來(lái)的做法是有問(wèn)題的。
這里應(yīng)該使用自己企業(yè)的域名地址,比如我的域名是amuxia.com(當(dāng)然這個(gè)也是假的,舉個(gè)例子,哈哈),這里就應(yīng)該配置smtp.amuxia.com。否則報(bào)錯(cuò):
javax.mail.AuthenticationFailedException:526Authentication failure[0]這里應(yīng)該注意一下。
3,郵箱實(shí)體類(lèi)(設(shè)置郵箱、郵件的相關(guān)信息)
public class EmailInfo { private final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; private String smtpServer; // SMTP服務(wù)器地址 private String port; // 端口 private String fromUserName; // 登錄SMTP服務(wù)器的用戶(hù)名,發(fā)送人郵箱地址 private String fromUserPassword; // 登錄SMTP服務(wù)器的密碼 private String toUser; // 收件人 private String subject; // 郵件主題 private String content; // 郵件正文 public EmailInfo() { } public EmailInfo(String toUser, String subject, String content) { this.toUser = toUser; this.subject = subject; this.content = content; this.smtpServer = Global.getConfig("smtpServer"); this.port = Global.getConfig("port"); this.fromUserName = Global.getConfig("fromUserName"); this.fromUserPassword = Global.getConfig("fromUserPassword"); } //get、set方法略 }
4,發(fā)送郵件的實(shí)現(xiàn)類(lèi)(工具類(lèi)):
public class EmailUtil { /** * 進(jìn)行base64加密,防止中文亂碼 */ private static String changeEncode(String str) { try { str = MimeUtility.encodeText(new String(str.getBytes(), "UTF-8"), "UTF-8", "B"); // "B"代表Base64 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return str; } public static boolean sendHtmlMail(EmailInfo emailInfo) { Properties properties = new Properties(); properties.put("mail.smtp.host", emailInfo.getSmtpServer()); properties.put("mail.transport.protocol", "smtp"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // 使用JSSE的SSL properties.put("mail.smtp.socketFactory.fallback", "false"); // 只處理SSL的連接,對(duì)于非SSL的連接不做處理 properties.put("mail.smtp.port", emailInfo.getPort()); properties.put("mail.smtp.socketFactory.port",emailInfo.getPort()); Session session = Session.getInstance(properties); session.setDebug(true); MimeMessage message = new MimeMessage(session); try { // 發(fā)件人 Address address = new InternetAddress(emailInfo.getFromUserName()); message.setFrom(address); // 收件人 Address toAddress = new InternetAddress(emailInfo.getToUser()); message.setRecipient(MimeMessage.RecipientType.TO, toAddress); // 設(shè)置收件人,并設(shè)置其接收類(lèi)型為T(mén)O // 主題message.setSubject(changeEncode(emailInfo.getSubject())); message.setSubject(emailInfo.getSubject()); // 時(shí)間 message.setSentDate(new Date()); Multipart multipart = new MimeMultipart(); // 創(chuàng)建一個(gè)包含HTML內(nèi)容的MimeBodyPart BodyPart html = new MimeBodyPart(); // 設(shè)置HTML內(nèi)容 html.setContent(emailInfo.getContent(), "text/html; charset=utf-8"); multipart.addBodyPart(html); // 將MiniMultipart對(duì)象設(shè)置為郵件內(nèi)容 message.setContent(multipart); message.saveChanges(); } catch (Exception e) { e.printStackTrace(); return false; } try { Transport transport = session.getTransport("smtp"); transport.connect(emailInfo.getSmtpServer(), emailInfo.getFromUserName(), emailInfo.getFromUserPassword()); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } }
5,測(cè)試一下:
public static void main(String[] args) { EmailUtil util = new EmailUtil(); String content =Global.getConfig("email_user_add_content"); content = content.format(content, "111","222"); System.out.println(content); EmailInfo info = new EmailInfo("amuxia@163.com", Global.getConfig("email_user_add_subject"), "<p>這是一個(gè)測(cè)試郵件</p>"); util.sendHtmlMail(info); }
這下就妥妥的了,這些代碼運(yùn)行沒(méi)有問(wèn)題,但是用到了其他的一些輔助類(lèi),如Global.getConfig()獲取配置文件中的信息,用時(shí)替換掉就行。
應(yīng)用中配置使用郵箱接收發(fā)送郵件,經(jīng)常會(huì)因?yàn)楦鱾€(gè)郵箱配置的細(xì)微差異出現(xiàn)錯(cuò)誤,多半是認(rèn)證不通過(guò),而認(rèn)證不通過(guò)的原因無(wú)非是:
1、服務(wù)器錯(cuò)誤
2、用戶(hù)名錯(cuò)誤
3、用戶(hù)名密碼不匹配。
遇到錯(cuò)誤從這幾方面下手就可以了,在代碼中使用郵箱發(fā)送郵件時(shí)要先在客戶(hù)端試一次,確保郵箱在客戶(hù)端是可接可收的。
這里順便說(shuō)一下怎么在outlook客戶(hù)端添加阿里云郵箱。1,點(diǎn)擊文件——》添加賬戶(hù)
2,選擇配置方式
3,填寫(xiě)賬戶(hù)信息:
4,點(diǎn)擊“其他設(shè)置”
最后就完成了配置,發(fā)送一封郵件測(cè)試一下就妥妥的了。
標(biāo)題名稱(chēng):Javamail配置阿里云郵箱發(fā)送郵件
標(biāo)題網(wǎng)址:http://chinadenli.net/article4/cjpooe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、商城網(wǎng)站、全網(wǎng)營(yíng)銷(xiāo)推廣、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)