JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
基于JavaWeb的郵件發(fā)送功能(多附件),供大家參考,具體內(nèi)容如下
本次學(xué)習(xí)主要目的是為了測(cè)試由QQ郵箱發(fā)送到任意一個(gè)有效郵箱的功能實(shí)現(xiàn),附帶多個(gè)附件。學(xué)習(xí)者可以借鑒其他郵箱的格式,梳理一下要寫(xiě)的內(nèi)容。項(xiàng)目把文件內(nèi)容放到了MimeMessage 郵件對(duì)象里,其中包含了如發(fā)件人、收件人、抄送人、郵件主題、郵件內(nèi)容、郵件時(shí)間和郵件附件等一些內(nèi)容。
項(xiàng)目中遇到的問(wèn)題:
1、在執(zhí)行到 File file = new File(“D:\Chat_Software\sky.JPG”);時(shí)出現(xiàn)錯(cuò)誤,之前寫(xiě)的時(shí)xlsx文件,測(cè)試期間可以對(duì).xls,jpg,文本,.doc文件進(jìn)行發(fā)送。發(fā)送xlsx文件時(shí)出現(xiàn)報(bào)錯(cuò)。
問(wèn)題解決方案:
.xls文件擴(kuò)展名對(duì)應(yīng)的是Microsoft Office EXCEL 2003及以前的版本。
.xlsx文件擴(kuò)展名對(duì)應(yīng)的是Microsoft Office EXCEL 2007及后期的版本。
有可能時(shí)你下載的mai不是1.6以上版本的,建議下載1.6以上版本的mail
2、在執(zhí)行到 message.saveChanges(); 方法報(bào)錯(cuò)無(wú)法進(jìn)行保存設(shè)置,也有可能時(shí)你的mail版本較低造成的。
在書(shū)寫(xiě) File file = new File(); 時(shí)注意修改正確的路徑,也可以寫(xiě)在form表單里用file進(jìn)行傳值,主題和內(nèi)容也寫(xiě)在了方法里因人而異如果其他需求可以需改參數(shù)進(jìn)行傳值。
本次用到的主要jar包如下:
- javax.mail-1.6.0.jar
- activation.jar
代碼如下:
EmailSendController.java
package com.yang.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.yang.util.Email_Send_Util; @Controller @RequestMapping("/email") public class EmailSendController { @RequestMapping(value = "/send.do") @ResponseBody public boolean impotr(HttpServletRequest request) { String toMail = request.getParameter("toMail"); String myMail = request.getParameter("myMail"); String userPwd = request.getParameter("userPwd"); System.out.println( toMail+myMail+userPwd); boolean bool=Email_Send_Util.send( toMail,myMail, userPwd); return bool ; } }
Authentication.java
package com.yang.util; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class Authentication extends Authenticator { String username = null; String password = null; public Authentication() { } public Authentication(String username, String password) { this.username = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ PasswordAuthentication pa = new PasswordAuthentication(username, password); return pa; } }
CreateMimeMessage.java
package com.yang.util; import java.io.File; import java.util.Date; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message.RecipientType; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; 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; /** * 創(chuàng)建一封復(fù)雜郵件(文本+圖片+附件) */ public class CreateMimeMessage { public static MimeMessage createMimeMessage(Session session, String myMail, String toMail) throws Exception { // 1. 創(chuàng)建郵件對(duì)象 MimeMessage message = new MimeMessage(session); // 2. From: 發(fā)件人 message.setFrom(new InternetAddress(myMail, "我的測(cè)試郵件_發(fā)件人昵稱(chēng)", "UTF-8")); // 3. To: 收件人(可以增加多個(gè)收件人、抄送、密送) message.addRecipient(RecipientType.TO, new InternetAddress(toMail, "我的測(cè)試郵件_收件人昵稱(chēng)", "UTF-8")); // 4. Subject: 郵件主題 message.setSubject("TEST郵件主題(文本+圖片+附件)", "UTF-8"); // 抄送人 Address ccAddress = new InternetAddress("*********@qq.com", "我的測(cè)試郵件_抄送人昵稱(chēng)", "UTF-8"); message.addRecipient(Message.RecipientType.CC, ccAddress); /* * 下面是郵件內(nèi)容的創(chuàng)建: */ // 5. 創(chuàng)建圖片“節(jié)點(diǎn)” MimeBodyPart image = new MimeBodyPart(); File file = new File("D:\\Chat_Software\\sky.JPG"); DataHandler dh = new DataHandler(new FileDataSource(file)); // 讀取本地文件 image.setDataHandler(dh); // 將圖片數(shù)據(jù)添加到“節(jié)點(diǎn)” // image.setContentID("image_fairy_tail");// 為“節(jié)點(diǎn)”設(shè)置一個(gè)唯一編號(hào)(在文本“節(jié)點(diǎn)”將引用該ID) image.setFileName(MimeUtility.encodeText(file.getName())); // 6. 創(chuàng)建文本“節(jié)點(diǎn)” MimeBodyPart text = new MimeBodyPart(); // text.setContent("這是一張圖片<br/>測(cè)試圖片<br/><img // src='cid:image_fairy_tail'/>", "text/html;charset=UTF-8"); text.setContent("這是一張圖片<br/>測(cè)試圖片", "text/html;charset=UTF-8"); // 7. (文本+圖片)設(shè)置 文本 和 圖片 “節(jié)點(diǎn)”的關(guān)系(將 文本 和 圖片 “節(jié)點(diǎn)”合成一個(gè)混合“節(jié)點(diǎn)”) MimeMultipart mm_text_image = new MimeMultipart(); mm_text_image.addBodyPart(text); mm_text_image.addBodyPart(image); mm_text_image.setSubType("related"); // 關(guān)聯(lián)關(guān)系 // 8. 將 文本+圖片 的混合“節(jié)點(diǎn)”封裝成一個(gè)普通“節(jié)點(diǎn)” // 最終添加到郵件的 Content 是由多個(gè) BodyPart 組成的 Multipart, 所以我們需要的是 BodyPart, // 上面的 mm_text_image 并非 BodyPart, 所有要把 mm_text_image 封裝成一個(gè) BodyPart MimeBodyPart text_image = new MimeBodyPart(); text_image.setContent(mm_text_image); // 9. 創(chuàng)建附件“節(jié)點(diǎn)” MimeBodyPart attachment = new MimeBodyPart(); File file2 = new File("E:\\boHaiBank\\Test\\test.xlsx"); DataHandler dh2 = new DataHandler(new FileDataSource(file2)); // 讀取本地文件 attachment.setDataHandler(dh2); // 將附件數(shù)據(jù)添加到“節(jié)點(diǎn)” attachment.setFileName(MimeUtility.encodeText(dh2.getName())); // 設(shè)置附件的文件名 // 10. 設(shè)置(文本+圖片)和 附件 的關(guān)系(合成一個(gè)大的混合“節(jié)點(diǎn)” / Multipart ) MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text_image); mm.addBodyPart(attachment); // 如果有多個(gè)附件,可以創(chuàng)建多個(gè)多次添加 mm.setSubType("mixed"); // 混合關(guān)系 // 11. 設(shè)置整個(gè)郵件的關(guān)系(將最終的混合“節(jié)點(diǎn)”作為郵件的內(nèi)容添加到郵件對(duì)象) message.setContent(mm); // 12. 設(shè)置發(fā)件時(shí)間 message.setSentDate(new Date()); // 13. 保存上面的所有設(shè)置 message.saveChanges(); return message; } }
Email_Send_Util.java
package com.yang.util; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.MimeMessage; public class Email_Send_Util { public static boolean send(String toMail,String myMail, String userPwd) { // QQ郵箱發(fā)件的服務(wù)器和端口 Properties props = new Properties(); props.put("mail.transport.protocol", "SMTP");// 設(shè)置發(fā)送郵件使用的協(xié)議 props.put("mail.smtp.host", "smtp.qq.com");// 指定郵件發(fā)送服務(wù)器服務(wù)器 "smtp.qq.com" props.put("mail.smtp.port", "25"); props.put("mail.smtp.auth", "true"); // 設(shè)置需要身份驗(yàn)證(不驗(yàn)證會(huì)不通過(guò)) Authenticator authentication = new Authentication(myMail, "你的郵箱授權(quán)碼"); Session session = Session.getDefaultInstance(props, authentication); MimeMessage message; try { message = CreateMimeMessage.createMimeMessage(session, myMail, toMail); // 獲取發(fā)送方對(duì)象 Transport transport = session.getTransport("smtp"); // 連接郵件服務(wù)器,鏈接您的QQ郵箱,用戶(hù)名(可以不用帶后綴)、密碼 transport.connect(myMail, userPwd); // 發(fā)送郵件 // 第一個(gè)參數(shù):郵件的消息體 // 第二個(gè)參數(shù):郵件所有的接收人/抄送人 transport.sendMessage(message, message.getAllRecipients()); transport.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
測(cè)試發(fā)送郵箱的jsp頁(yè)面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>郵件發(fā)送</title> <script type="text/javascript" src="js/jquery-1.10.2.js"></script> <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script> </head> <body> <div> <form id="login_form" method="post"> <table border="1px" width="750px" height="400px" align="center" cellpadding="0px" cellspacing="0px" bgcolor="white" > <tr height="40px"> <td colspan="2"> <font size="4">郵件發(fā)送</font> Email </td> </tr> <tr> <td>收件人</td><td><input type="text" name="toMail" size="34px"/></td> </tr> <tr> <td>郵件發(fā)送人</td><td><input type="text" name="myMail" size="34px"/></td> </tr> <tr> <td>密碼</td><td><input type="text" name="userPwd" size="34px"/></td> </tr> </table> <input type="button" onclick="emailsend()" value="發(fā)送"> </form> </div> <script type="text/javascript"> function emailsend() { $.ajax({ url : "email/send.do", type : "POST", data : $("#login_form").serialize(), beforeSend : function() { console.log("正在進(jìn)行,請(qǐng)稍候"); }, success : function(e) { if (e == true) { alert("發(fā)送成功"); } else { alert("發(fā)送失敗"); } } }); } </script> </body> </html>
發(fā)送成功后,收件人的郵件
所需的jar包可以在這個(gè)鏈接里下載,其中還有定時(shí)發(fā)送郵件所需的jar包:mail.jar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中實(shí)現(xiàn)線程的三種方式及對(duì)比_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
本文給大家分享了java實(shí)現(xiàn)線程的三種方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-05-05spring boot中的properties參數(shù)配置詳解
這篇文章主要介紹了spring boot中的properties參數(shù)配置,需要的朋友可以參考下2017-09-09關(guān)于pytorch相關(guān)部分矩陣變換函數(shù)的問(wèn)題分析
這篇文章主要介紹了pytorch相關(guān)部分矩陣變換函數(shù),包括tensor維度順序變換BCHW順序的調(diào)整,矩陣乘法相關(guān)函數(shù),矩陣乘,點(diǎn)乘,求取矩陣對(duì)角線元素或非對(duì)角線元素的問(wèn)題,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03Java通過(guò)FTP服務(wù)器上傳下載文件的方法
本文介紹了如何使用Apache Jakarta Commons Net(commons-net-3.3.jar)基于FileZilla Server服務(wù)器實(shí)現(xiàn)FTP服務(wù)器上文件的上傳/下載/刪除等操作,需要的朋友可以參考下2015-07-07關(guān)于Jsoup將相對(duì)路徑轉(zhuǎn)為絕對(duì)路徑的方法
這篇文章主要介紹了關(guān)于Jsoup將相對(duì)路徑轉(zhuǎn)為絕對(duì)路徑的方法,jsoup 是一款Java 的HTML解析器,可直接解析某個(gè)URL地址、HTML文本內(nèi)容,需要的朋友可以參考下2023-04-04springboot jpaRepository為何一定要對(duì)Entity序列化
這篇文章主要介紹了springboot jpaRepository為何一定要對(duì)Entity序列化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12詳解spring boot jpa整合QueryDSL來(lái)簡(jiǎn)化復(fù)雜操作
這篇文章主要介紹了詳解spring boot jpa整合QueryDSL來(lái)簡(jiǎn)化復(fù)雜操作,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Java解決前端數(shù)據(jù)處理及亂碼問(wèn)題
大伙們有沒(méi)有遇到數(shù)據(jù)亂碼的問(wèn)題,真的是讓人心情煩躁,今天就來(lái)教下大家數(shù)據(jù)怎么傳輸?shù)角岸艘约皝y碼問(wèn)題怎么解決的,需要的朋友可以參考一下2021-12-12