一、郵件的相關概念
郵件協(xié)議。主要包括:
SMTP協(xié)議:Simple Mail Transfer Protocol,即簡單郵件傳輸協(xié)議,用于發(fā)送電子郵件
POP3協(xié)議:Post Office Protocol 3,即郵局協(xié)議的第三個版本,用于接收郵件
IMAP協(xié)議:Internet Message Access Protocol,即互聯(lián)網(wǎng)消息訪問協(xié)議,是POP3的替代協(xié)議
--------------------------------------------------------------------------------
二、搭建James郵件服務器
James是Apache的一個開源項目,純Java實現(xiàn)
搭建James服務器
① 下載apache-james-2.3.2.zip解壓
② 運行bin目錄下的run.bat即可啟動服務器[Telnet localhost 4555]
③ 通過apps\james\SAR-INF\config.xml配置服務器
注:先到bin下run一道。 放如非中文目錄, 得再控制面板開啟Telnet客戶端
--------------------------------------------------------------------------------
三、安裝OutLook[郵件客戶端]
產(chǎn)品秘鑰:PQDV9-GPDV4-CRM4D-PHDTH-4M2MT
創(chuàng)建用戶賬號
一、使用telnet連接James的Remote Administration Tool
二、以管理員身份登錄
三、使用adduser命令添加用戶
--------------------------------------------------------------------------------
四、配置outlook郵件客戶端
為了方便查看,可以配置Microsoft Outlook郵件客戶端,保證James郵件服務器是啟動狀態(tài),啟動Microsoft Outlook.
選擇“工具”->“選項”,打開“選項”面板。選擇“郵件設置”并點擊“電子郵件賬戶”,打開“賬號設置”面板。在“電子郵件”選項卡下新建郵件賬戶
--------------------------------------------------------------------------------
五、案例[搭建James郵件服務器]
需求說明:
在本機搭建James郵件服務器,自定義服務器的名稱。
創(chuàng)建兩個測試用戶。
在Microsoft Outlook中配置其中一個測試用戶為Outlook郵件賬戶
--------------------------------------------------------------------------------
六、使用JavaMail發(fā)送電子郵件(案例)
需求:
使用JavaMail技術,實現(xiàn)從A賬戶給B賬戶發(fā)送一封電子郵件,標題為“會議通知”,郵件內(nèi)容為“XX你好!請于明天下午16:00 準時到B01會議室召開技術討論會。”通過Outlook 客戶端查看郵件程序發(fā)送的郵件是否發(fā)送成功
關鍵代碼:
創(chuàng)建一個類EmailAuthenticator并繼承自Authenticator,并植入用戶名和密碼
創(chuàng)建Mail類設置郵件信息:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
public class Mail { private String mailServer,from,to,mailSubject,mailContent; private String username,password; public Mail(){ //設置郵件信息 //進行認證登錄的用戶名 //認證密碼 password= "hq" ; //認證的郵箱對應的郵件服務器 mailServer= "192.168.17.176" ; //發(fā)件人信息 from= "wj" ; //收件人信息 //郵件標題 mailSubject= "我們都是好孩子333" ; //郵件內(nèi)容 mailContent= "這是一封測試郵件!如有雷同,純屬不可能" ; } //設置郵件服務器 @SuppressWarnings ( "static-access" ) public void send(){ Properties prop=System.getProperties(); //指定郵件server prop.put( "mail.smtp.host" , mailServer); //是否開啟認證 prop.put( "mail.smtp.auth" , "true" ); //smtp協(xié)議的 prop.put( "mail.smtp.port" , "25" ); //產(chǎn)生Session服務 EmailAuthenticator mailauth= new EmailAuthenticator(username, password); Session mailSession=Session.getInstance(prop,(Authenticator)mailauth); try { //封裝Message對象 Message message= new MimeMessage(mailSession); message.setFrom( new InternetAddress(from)); //發(fā)件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //收件人 message.setSubject(mailSubject); //設置內(nèi)容(設置字符集處理亂碼問題) message.setContent(mailContent, "text/html;charset=gbk" ); message.setSentDate( new Date()); //創(chuàng)建Transport實例,發(fā)送郵件 Transport tran=mailSession.getTransport( "smtp" ); tran.send(message,message.getAllRecipients()); tran.close(); } catch (Exception e) { e.printStackTrace(); } } |
測試類:
1
2
3
4
5
6
7
8
9
10
11
12
|
public class MyTest { public static void main(String[] args) { Mail mail= new Mail(); mail.send(); System.out.println( "success!" ); } } |
--------------------------------------------------------------------------------
七、發(fā)送帶附件的Mail
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
|
public class MailWithAttachment { private JavaMailSender mailSender; //必須使用 JavaMailSender public void setMailSender(JavaMailSender mailSender) { this .mailSender = mailSender; } public void send() throws MessagingException,IOException{ MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true , "UTF-8" ); helper.setSubject( "哈哈哈" ); helper.setText( "每日一笑,開開心心!!!" ); //添加附件1 ClassPathResource file1 = new ClassPathResource( "/cn/bdqn/attachfiles/test.doc" ); helper.addAttachment(file1.getFilename(), file1.getFile()); //添加附件2:附件的文件名為中文時,需要對文件名進行編碼轉換,解決亂碼問題 ClassPathResource file2 = new ClassPathResource( "/cn/bdqn/attachfiles/附件測試文件.doc" ); helper.addAttachment(MimeUtility.encodeWord(file2.getFilename()),file2.getFile()); mailSender.send(mimeMessage); } } |
測試類:
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
|
public class MailTest { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); /*測試帶附件的郵件*/ try { MailWithAttachment mailWithAttach = (MailWithAttachment)context.getBean( "mailWithAttachment" ); mailWithAttach.send(); } catch (Exception e){ System.out.print(e.toString()); } } } |
applicationContext.xml:大配置
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/WJ-163/p/6187188.html