本篇介紹基于SSM框架(Spring4.0+SpringMVC+Mybatis)組合的Javamail應用,郵箱的話基于騰訊的QQ郵箱,其實也是Foxmail郵箱
先要了解一下SMTP協議和SSL加密
SMTP:稱為簡單郵件傳輸協議(Simple Mail Transfer Protocal),目標是向用戶提供高效、可靠的郵件傳輸。SMTP是一種請求響應的協議,也就是客戶機向遠程服務器發送請求,服務器響應,監聽端口是25,所以其工作模式有兩種:發送SMTP,接收SMTP。
SSL加密:用來保障瀏覽器和網站服務器的安全性,其原理用譯文解釋就是:
當你的瀏覽器向服務器請求一個安全的網頁(通常是 https://)
服務器就把它的證書和公匙發回來
瀏覽器檢查證書是不是由可以信賴的機構頒發的,確認證書有效和此證書是此網站的。
使用公鑰加密了一個隨機對稱密鑰,包括加密的URL一起發送到服務器
服務器用自己的私匙解密了你發送的鑰匙。然后用這把對稱加密的鑰匙給你請求的URL鏈接解密。
服務器用你發的對稱鑰匙給你請求的網頁加密。你也有相同的鑰匙就可以解密發回來的網頁了
然后介紹怎么實現javamail發送郵件,先要下載javamail的jar:mail.jar
去充當服務器的QQ郵箱開啟SMTP服務:
寫個發送郵件的業務類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package com.appms.email; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.sun.mail.util.MailSSLSocketFactory; public class JavaEmailSender { public static void sendEmail(String toEmailAddress,String emailTitle,String emailContent) throws Exception{ Properties props = new Properties(); // 開啟debug調試 props.setProperty( "mail.debug" , "true" ); // 發送服務器需要身份驗證 props.setProperty( "mail.smtp.auth" , "true" ); // 設置郵件服務器主機名 props.setProperty( "mail.host" , "smtp.qq.com" ); // 發送郵件協議名稱 props.setProperty( "mail.transport.protocol" , "smtp" ); /**SSL認證,注意騰訊郵箱是基于SSL加密的,所有需要開啟才可以使用**/ MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts( true ); props.put( "mail.smtp.ssl.enable" , "true" ); props.put( "mail.smtp.ssl.socketFactory" , sf); //創建會話 Session session = Session.getInstance(props); //發送的消息,基于觀察者模式進行設計的 Message msg = new MimeMessage(session); msg.setSubject(emailTitle); //使用StringBuilder,因為StringBuilder加載速度會比String快,而且線程安全性也不錯 StringBuilder builder = new StringBuilder(); builder.append( " " +emailContent); builder.append( " 時間 " + new Date()); msg.setText(builder.toString()); msg.setFrom( new InternetAddress( "你的QQ郵箱" )); Transport transport = session.getTransport(); transport.connect( "smtp.qq.com" , "你的QQ郵箱" , "你開啟SMTP服務申請的獨立密碼" ); //發送消息 transport.sendMessage(msg, new Address[] { new InternetAddress(toEmailAddress) }); transport.close(); } } |
然后寫個SpringMVC框架的Controller類:
- /**
- * 跳轉到發送郵件頁面
- * @return
- * @throws Exception
- */
- @RequestMapping("/goSendEmail")
- public ModelAndView goSendEmail(HttpServletRequest request)throws Exception{
- ModelAndView mv = this.getModelAndView();
- String email = request.getParameter("email");
- if(email!=null&&!"".equals(email)){
- email = email.trim();
- mv.setViewName("member/send_email");
- mv.addObject("email", email);
- }
- return mv;
- }
- /**
- * 發送郵件
- * @return
- * @throws Exception
- */
- @RequestMapping(value="/sendEmail",produces="application/json;charset=UTF-8")
- @ResponseBody
- public Object sendEmail(HttpServletRequest request)throws Exception{
- Map<String,String> map = new HashMap<String,String>();
- String msg = "ok"; //發送狀態
- String toEMAIL = request.getParameter("EMAIL"); //對方郵箱
- String TITLE = request.getParameter("TITLE"); //標題
- String CONTENT = request.getParameter("CONTENT"); //內容
- JavaEmailSender.sendEmail(toEMAIL, TITLE, CONTENT);
- map.put("result", msg);
- return map;
- }
這里用了jQuery TIP插件進行驗證提示,所以需要引入相應的Jquery文件
- <script type="text/javascript" src="source/js/jquery-1.7.2.js"></script>
- <!--提示框-->
- <script type="text/javascript" src="source/js/jquery.tips.js"></script>
Jquery表單驗證和Ajax異步請求:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<!-- 發送郵件 --> <script type= "text/javascript" > //發送 function sendEm(){ if ($( "#TYPE" ).val()== "1" ){ $( "#CONTENT" ).val(getContentTxt()); } else { $( "#CONTENT" ).val(getContent()); } if ($( "#EMAIL" ).val()== "" ){ $( "#EMAIL" ).tips({ side:3, msg: '請輸入郵箱' , bg: '#AE81FF' , time:2 }); $( "#EMAIL" ).focus(); return false ; } if ($( "#TITLE" ).val()== "" ){ $( "#TITLE" ).tips({ side:3, msg: '請輸入標題' , bg: '#AE81FF' , time:2 }); $( "#TITLE" ).focus(); return false ; } if ($( "#CONTENT" ).val()== "" ){ $( "#nr" ).tips({ side:1, msg: '請輸入內容' , bg: '#AE81FF' , time:3 }); return false ; } var EMAIL = $( "#EMAIL" ).val(); var TYPE = $( "#TYPE" ).val(); var TITLE = $( "#TITLE" ).val(); var CONTENT = $( "#CONTENT" ).val(); $( "#zhongxin" ).hide(); $( "#zhongxin2" ).show(); $.ajax({ type: "POST" , url: 'retroaction/sendEmail.do?tm=' + new Date().getTime(), data: {EMAIL:EMAIL,TITLE:TITLE,CONTENT:CONTENT}, dataType: 'json' , //beforeSend: validateData, cache: false , success: function (data){ if ( "ok" == data.result){ $( "#msg" ).tips({ side:3, msg: '發送成功!' , bg: '#68B500' , time:5 }); setTimeout( "showdiv()" ,1000); } else { $( "#msg" ).tips({ side:3, msg: '發送失敗!' , bg: '#68B500' , time:5 }); } } }); } </script> |
JSP頁面的調用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!-- 編輯郵箱 --> <div> <table style= "width:98%;" > <tr> <td style= "margin-top:0px;" > <div style= "float: left;" style= "width:81%" ><textarea name= "EMAIL" id= "EMAIL" rows= "1" cols= "50" style= "width:600px;height:20px;" placeholder= "請選輸入對方郵箱,多個請用(;)分號隔開" title= "請選輸入對方郵箱,多個請用(;)分號隔開" >${email}</textarea></div> <div style= "float: right;" style= "width:19%" ><a class= 'btn btn-mini btn-info' title= "編輯郵箱" onclick= "dialog_open();" >編輯郵箱</i></a></div> </td> </tr> <tr> <td> <input type= "text" name= "TITLE" id= "TITLE" value= "" placeholder= "請選輸入郵件標題" style= "width:98%" /> </td> </tr> <tr> <td id= "nr" > <script id= "editor" type= "text/plain" style= "width:650px;height:259px;" ></script> </td> </tr> <tr> <td style= "text-align: center;" > <a class= "btn btn-mini btn-primary" onclick= "sendEm();" >發送</a> <a class= "btn btn-mini btn-danger" onclick= "top.Dialog.close();" >取消</a> </td> </tr> </table> </div> <div id= "zhongxin2" class= "center" style= "display:none" ><br/><img src= "assets/images/jzx.gif" id= 'msg' /><br/><h4 class= "lighter block green" id= 'msg' >正在發送...</h4></div> |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/u014427391/article/details/53887801