本文實例為大家分享了郵件收發功能的具體實現代碼,供大家參考,具體內容如下
準備工作, 環境搭建:
1. 本地搭建一個郵件服務器
易郵服務器,eyoumailserversetup.exe
2. 新建郵箱賬號
張三給李四發郵件。
步驟1:
新建域名: 工具, 服務器設置, 單域名框中輸入 itcast.com
步驟2:
新建郵箱賬號: [email protected]
[email protected]
3. 安裝foxmail
配置郵件發送服務器(smtp): localhost 25
郵件接收服務器(pop3): localhost 110
再新建賬號,就可以接收郵件了!
注意
如果是web項目,因為javaee自帶的有郵件功能,可能存在問題!
我們要用自己的mail.jar文件功能! 需要刪除javaee中mail包!
使用:
JavaMail開發,先引入jar文件:
activation.jar 【如果使用jdk1.6或以上版本,可以不用這個jar文件】
mail.jar 【郵件發送核心包】
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
|
/** * 1. 發送一封普通郵件 * @author Jie.Yuan * */ public class App_SendMail { @Test public void testSend() throws Exception { //0. 郵件參數 Properties prop = new Properties(); prop.put( "mail.transport.protocol" , "smtp" ); // 指定協議 prop.put( "mail.smtp.host" , "localhost" ); // 主機 stmp.qq.com prop.put( "mail.smtp.port" , 25 ); // 端口 prop.put( "mail.smtp.auth" , "true" ); // 用戶密碼認證 prop.put( "mail.debug" , "true" ); // 調試模式 //1. 創建一個郵件的會話 Session session = Session.getDefaultInstance(prop); //2. 創建郵件體對象 (整封郵件對象) MimeMessage message = new MimeMessage(session); //3. 設置郵件體參數: //3.1 標題 message.setSubject( "我的第一封郵件 " ); //3.2 郵件發送時間 message.setSentDate( new Date()); //3.3 發件人 //3.4 接收人 //3.5內容 message.setText( "你好,已經發送成功! 正文...." ); // 簡單純文本郵件 message.saveChanges(); // 保存郵件(可選) //4. 發送 Transport trans = session.getTransport(); trans.connect( "zhangsan" , "888" ); // 發送郵件 trans.sendMessage(message, message.getAllRecipients()); trans.close(); } } |
帶圖片
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
|
/** * 帶圖片資源的郵件 * @author Jie.Yuan * */ public class App_2SendWithImg { // 初始化參數 private static Properties prop; // 發件人 private static InternetAddress sendMan = null ; static { prop = new Properties(); prop.put( "mail.transport.protocol" , "smtp" ); // 指定協議 prop.put( "mail.smtp.host" , "localhost" ); // 主機 stmp.qq.com prop.put( "mail.smtp.port" , 25 ); // 端口 prop.put( "mail.smtp.auth" , "true" ); // 用戶密碼認證 prop.put( "mail.debug" , "true" ); // 調試模式 try { } catch (AddressException e) { throw new RuntimeException(e); } } @Test public void testSend() throws Exception { // 1. 創建郵件會話 Session session = Session.getDefaultInstance(prop); // 2. 創建郵件對象 MimeMessage message = new MimeMessage(session); // 3. 設置參數:標題、發件人、收件人、發送時間、內容 message.setSubject( "帶圖片郵件" ); message.setSender(sendMan); message.setSentDate( new Date()); /***************設置郵件內容: 多功能用戶郵件 (related)*******************/ // 4.1 構建一個多功能郵件塊 MimeMultipart related = new MimeMultipart( "related" ); // 4.2 構建多功能郵件塊內容 = 左側文本 + 右側圖片資源 MimeBodyPart content = new MimeBodyPart(); MimeBodyPart resource = new MimeBodyPart(); // 設置具體內容: a.資源(圖片) String filePath = App_2SendWithImg. class .getResource( "8.jpg" ).getPath(); DataSource ds = new FileDataSource( new File(filePath)); DataHandler handler = new DataHandler(ds); resource.setDataHandler(handler); resource.setContentID( "8.jpg" ); // 設置資源名稱,給外鍵引用 // 設置具體內容: b.文本 content.setContent( "<img src='cid:8.jpg'/> 好哈哈!" , "text/html;charset=UTF-8" ); related.addBodyPart(content); related.addBodyPart(resource); /*******4.3 把構建的復雜郵件快,添加到郵件中********/ message.setContent(related); // 5. 發送 Transport trans = session.getTransport(); trans.connect( "zhangsan" , "888" ); trans.sendMessage(message, message.getAllRecipients()); trans.close(); } } |
圖片+附件
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
|
/** * 3. 帶圖片資源以及附件的郵件 * @author Jie.Yuan * */ public class App_3ImgAndAtta { // 初始化參數 private static Properties prop; // 發件人 private static InternetAddress sendMan = null ; static { prop = new Properties(); prop.put( "mail.transport.protocol" , "smtp" ); // 指定協議 prop.put( "mail.smtp.host" , "localhost" ); // 主機 stmp.qq.com prop.put( "mail.smtp.port" , 25 ); // 端口 prop.put( "mail.smtp.auth" , "true" ); // 用戶密碼認證 prop.put( "mail.debug" , "true" ); // 調試模式 try { } catch (AddressException e) { throw new RuntimeException(e); } } @Test public void testSend() throws Exception { // 1. 創建郵件會話 Session session = Session.getDefaultInstance(prop); // 2. 創建郵件對象 MimeMessage message = new MimeMessage(session); // 3. 設置參數:標題、發件人、收件人、發送時間、內容 message.setSubject( "帶圖片郵件" ); message.setSender(sendMan); message.setSentDate( new Date()); /* * 帶附件(圖片)郵件開發 */ // 構建一個總的郵件塊 MimeMultipart mixed = new MimeMultipart("mixed"); // ---> 總郵件快,設置到郵件對象中 message.setContent(mixed); // 左側: (文本+圖片資源) MimeBodyPart left = new MimeBodyPart(); // 右側: 附件 MimeBodyPart right = new MimeBodyPart(); // 設置到總郵件塊 mixed.addBodyPart(left); mixed.addBodyPart(right); /******附件********/ String attr_path = this.getClass().getResource("a.docx").getPath(); DataSource attr_ds = new FileDataSource(new File(attr_path)); DataHandler attr_handler = new DataHandler(attr_ds); right.setDataHandler(attr_handler); right.setFileName("a.docx"); /***************設置郵件內容: 多功能用戶郵件 (related)*******************/ // 4.1 構建一個多功能郵件塊 MimeMultipart related = new MimeMultipart( "related" ); // ----> 設置到總郵件快的左側中 left.setContent(related); // 4.2 構建多功能郵件塊內容 = 左側文本 + 右側圖片資源 MimeBodyPart content = new MimeBodyPart(); MimeBodyPart resource = new MimeBodyPart(); // 設置具體內容: a.資源(圖片) String filePath = App_3ImgAndAtta. class .getResource( "8.jpg" ).getPath(); DataSource ds = new FileDataSource( new File(filePath)); DataHandler handler = new DataHandler(ds); resource.setDataHandler(handler); resource.setContentID( "8.jpg" ); // 設置資源名稱,給外鍵引用 // 設置具體內容: b.文本 content.setContent( "<img src='cid:8.jpg'/> 好哈哈!" , "text/html;charset=UTF-8" ); related.addBodyPart(content); related.addBodyPart(resource); // 5. 發送 Transport trans = session.getTransport(); trans.connect( "zhangsan" , "888" ); trans.sendMessage(message, message.getAllRecipients()); trans.close(); } } |
以上就是本文的全部內容,希望對大家學習java程序設計有所幫助。