發(fā)送郵件的功能比較簡單,主要是對郵件進(jìn)行配置,包括連接時的協(xié)議,權(quán)限驗證,創(chuàng)建會話,郵件主題和內(nèi)容,發(fā)送方和接收方等信息,最后發(fā)送即可。

主要涉及的類有:
Properties:設(shè)置協(xié)議,權(quán)限等;
Session:用來創(chuàng)建會話,也就是程序到郵件服務(wù)器的第一次對話;
Message:設(shè)置郵件內(nèi)容,發(fā)送方和接收方等。
激活賬號的時候一般都會有一個激活碼,這個激活碼就是用戶創(chuàng)建賬號的時候給用戶設(shè)置的一個屬性值,當(dāng)用戶激活的時候我們再根據(jù)這個屬性值去查詢用戶,同時把用戶的另一個屬性值比如”status”從”0”更新為”1”,然后在用戶登錄的時候不僅要驗證用戶名和密碼,還要驗證這個”status”的值是不是為”1”,這樣才能正常登錄。

2,mail功能編寫

需要的jar包:mail.jar

public class MailUtils { //String email:用戶用來激活的郵箱 //String emailMsg:郵件內(nèi)容 public static void sendMail(String email, String emailMsg) throws AddressException, MessagingException, GeneralSecurityException, UnsupportedEncodingException{ // 1.創(chuàng)建一個程序與郵件服務(wù)器會話對象 Session Properties props = new Properties(); //設(shè)置連接時的協(xié)議為”SMTP” props.setProperty(“mail.transport.protocol”, “SMTP”); //主機(jī),qq郵箱就是”smtp.qq.com”,163郵箱就是”smtp.163.com” props.setProperty(“mail.host”, “smtp.qq.com”); //開啟權(quán)限驗證 props.setProperty(“mail.smtp.auth”, “true”); //ssl加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.setProperty(“mail.smtp.ssl.enable”, “true”); props.put(“mail.smtp.ssl.socketFactory”, sf); // 創(chuàng)建驗證器 Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(“122918552@qq.com”, “郵箱授權(quán)碼”); } }; //創(chuàng)建程序到郵件服務(wù)器的第一次對話 Session session = Session.getInstance(props, auth); //控制臺輸出debug信息 session.setDebug(true); // 2.創(chuàng)建一個Message,它相當(dāng)于郵件內(nèi)容 //相當(dāng)于獲取信封 Message message = new MimeMessage(session); //設(shè)置發(fā)送人 message.setFrom(new InternetAddress(“122918552@qq.com”)); //設(shè)置發(fā)送方式與接收者 message.setRecipient(RecipientType.TO, new InternetAddress(email)); //郵件主題 message.setSubject(“activateMail”); //郵件內(nèi)容 message.setContent(emailMsg, “text/html;charset=utf-8″); // 3.創(chuàng)建 Transport用來發(fā)送郵件 Transport.send(message); } } 3,jsp編寫

<form action=”${pageContext.request.contextPath}/regist” method=”post”> <input type=”hidden” name=”method” value=”regist”/> <table align=”center” width=”30%”> … <tr> <td>郵箱</td> <td><input id=”email_id” type=”text” name=”email”/> </td> </tr> <tr> <td align=”center” > <button id=”regist_id” type=”submit”>注冊</button> </td> &nbsp;&nbsp;&nbsp;&nbsp; <td><button id=”cancle_id” type=”button”>取消</button> </td> </tr> </table> </form>

”form”表單中有個一隱藏的”input”,我們可以根據(jù)隱藏域中的”name”屬性去獲取”value”,然后在后臺判斷這個”value”,去做對應(yīng)的操作,其實(shí)就是對請求進(jìn)行統(tǒng)一管理。

4,servlet編寫

”servlet”中首先對請求類型進(jìn)行判斷,上邊的隱藏域這時候就用到了,這里主要模擬一個激活碼,實(shí)際當(dāng)中這個激活碼是在用戶注冊賬號的時候生成的,比如用”UUID”隨機(jī)生成一串字符作為激活碼。

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ request.setCharacterEncoding(“utf-8”); String method = request.getParameter(“method”); if(“regist”.equals(method)) { //注冊操作 regist(request, response); }else if(“active”.equals(method)) { //激活操作 active(request, response); } } public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //這里是用戶注冊之后顯示的頁面 request.getRequestDispatcher(“registLater.jsp”).forward(request, response); //獲取用戶輸入的email String email = request.getParameter(“email”); //模擬一個激活碼”1234″ String activeCode = “1234”; //設(shè)置郵件內(nèi)容,注意鏈接中的”method” String emailMsg = “注冊成功,請點(diǎn)擊<a href=\\\’http://localhost:8080/emailActivate/regist?method=active&activeCode=” activeCode “\\\’>激活</a>,驗證碼是” activeCode; System.out.println(“正在發(fā)送郵件…”); try { //發(fā)送郵件 MailUtils.sendMail(email, emailMsg); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } System.out.println(“發(fā)送郵件成功”); } public void active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //從激活請求鏈接中獲取”activeCode” String activeCode = request.getParameter(“activeCode”); if(“1234”.equals(activeCode)) { System.out.println(“激活成功”); //進(jìn)入到激活成功的頁面 request.getRequestDispatcher(“activeSuccess.jsp”).forward(request, response); } } 5,常見問題 5.1 5.1.1,問題描述

530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28 javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl).More information at http://service.mail.qq.com/cgi-bin/help?id=28

5.1.2,問題原因:

沒有ssl加密

5.1.3,解決辦法:

MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.setProperty(“mail.smtp.ssl.enable”, “true”); props.put(“mail.smtp.ssl.socketFactory”, sf); 5.2 5.2.1,問題描述

535 Error: 請使用授權(quán)碼登錄。詳情請看: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
javax.mail.AuthenticationFailedException: 535 Error: ?¨?ê?é: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

5.2.2,問題原因:

這個就很明顯了,需要使用授權(quán)碼登錄(此處用的是QQ郵箱發(fā)送文件)

5.2.3,解決辦法:

進(jìn)入QQ郵箱首頁,點(diǎn)擊設(shè)置->賬戶

然后往下翻頁,

把第一個“開啟”,按照提示操作,最后會生成一個授權(quán)碼,寫入PasswordAuthentication(“郵箱賬號”, “授權(quán)碼”)就OK了,如果還有問題,把圖中的第二個也開啟,兩個授權(quán)碼不一樣,但是都可以用。

如果,你對上面的內(nèi)容還有疑問,推薦選擇西部數(shù)碼企業(yè)云郵箱!有專人協(xié)助您解答郵箱疑問。

西部數(shù)碼21年老牌服務(wù)商,企業(yè)郵箱功能豐富,如定時發(fā)送、支持層級子文件夾,郵件撤回,日程微信通知、多彩便簽等幾十項特色功能,使用管理便捷。按需自由定制,購買靈活,PCIe加速、極速收發(fā)!而且支持小程序收發(fā)郵件,隨時隨地移動辦公。價格實(shí)惠,還可以免費(fèi)試用!

高性價比企業(yè)郵箱開通鏈接:http://www.bingfeng168.cn/services/mail/

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-62778877-8306;郵箱:fanjiao@west.cn。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明出處:西部數(shù)碼知識庫 » java mail郵箱驗證

登錄

找回密碼

注冊