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

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

2,mail功能編寫(xiě)

需要的jar包:mail.jar

public class MailUtils { //String email:用戶(hù)用來(lái)激活的郵箱 //String emailMsg:郵件內(nèi)容 public static void sendMail(String email, String emailMsg) throws AddressException, MessagingException, GeneralSecurityException, UnsupportedEncodingException{ // 1.創(chuàng)建一個(gè)程序與郵件服務(wù)器會(huì)話(huà)對(duì)象 Session Properties props = new Properties(); //設(shè)置連接時(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”); //開(kāi)啟權(quán)限驗(yà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)建驗(yàn)證器 Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(“122918552@qq.com”, “郵箱授權(quán)碼”); } }; //創(chuàng)建程序到郵件服務(wù)器的第一次對(duì)話(huà) Session session = Session.getInstance(props, auth); //控制臺(tái)輸出debug信息 session.setDebug(true); // 2.創(chuàng)建一個(gè)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用來(lái)發(fā)送郵件 Transport.send(message); } } 3,jsp編寫(xiě)

<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”>注冊(cè)</button> </td> &nbsp;&nbsp;&nbsp;&nbsp; <td><button id=”cancle_id” type=”button”>取消</button> </td> </tr> </table> </form>

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

4,servlet編寫(xiě)

”servlet”中首先對(duì)請(qǐng)求類(lèi)型進(jìn)行判斷,上邊的隱藏域這時(shí)候就用到了,這里主要模擬一個(gè)激活碼,實(shí)際當(dāng)中這個(gè)激活碼是在用戶(hù)注冊(cè)賬號(hào)的時(shí)候生成的,比如用”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)) { //注冊(cè)操作 regist(request, response); }else if(“active”.equals(method)) { //激活操作 active(request, response); } } public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //這里是用戶(hù)注冊(cè)之后顯示的頁(yè)面 request.getRequestDispatcher(“registLater.jsp”).forward(request, response); //獲取用戶(hù)輸入的email String email = request.getParameter(“email”); //模擬一個(gè)激活碼”1234″ String activeCode = “1234”; //設(shè)置郵件內(nèi)容,注意鏈接中的”method” String emailMsg = “注冊(cè)成功,請(qǐng)點(diǎn)擊<a href=\\\’http://localhost:8080/emailActivate/regist?method=active&activeCode=” activeCode “\\\’>激活</a>,驗(yàn)證碼是” 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 { //從激活請(qǐng)求鏈接中獲取”activeCode” String activeCode = request.getParameter(“activeCode”); if(“1234”.equals(activeCode)) { System.out.println(“激活成功”); //進(jìn)入到激活成功的頁(yè)面 request.getRequestDispatcher(“activeSuccess.jsp”).forward(request, response); } } 5,常見(jiàn)問(wèn)題 5.1 5.1.1,問(wèn)題描述

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,問(wèn)題原因:

沒(méi)有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,問(wèn)題描述

535 Error: 請(qǐng)使用授權(quán)碼登錄。詳情請(qǐng)看: 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,問(wèn)題原因:

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

5.2.3,解決辦法:

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

然后往下翻頁(yè),

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

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

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

高性?xún)r(jià)比企業(yè)郵箱開(kāi)通鏈接:http://bingfeng168.cn/services/mail/

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

登錄

找回密碼

注冊(cè)