使用JavaMail發送郵件


一、郵件的相關概念

郵件協議。主要包括:

SMTP協議:Simple Mail Transfer Protocol,即簡單郵件傳輸協議,用於發送電子郵件

POP3協議:Post Office Protocol 3,即郵局協議的第三個版本,用於接收郵件

IMAP協議:Internet Message Access Protocol,即互聯網消息訪問協議,是POP3的替代協議

 


 二、搭建James郵件服務器

 James是Apache的一個開源項目,純Java實現

 搭建James服務器

 ① 下載apache-james-2.3.2.zip解壓

 ② 運行bin目錄下的run.bat即可啟動服務器[Telnet  localhost 4555]

 ③ 通過apps\james\SAR-INF\config.xml配置服務器

 注:先到bin下run一道。 放如非中文目錄, 得再控制面板開啟Telnet客戶端

 


 三、安裝OutLook[郵件客戶端]

產品秘鑰:PQDV9-GPDV4-CRM4D-PHDTH-4M2MT

創建用戶賬號

一、使用telnet連接James的Remote Administration Tool

二、以管理員身份登錄

三、使用adduser命令添加用戶


四、配置outlook郵件客戶端

為了方便查看,可以配置Microsoft Outlook郵件客戶端,保證James郵件服務器是啟動狀態,啟動Microsoft Outlook.

選擇“工具”->“選項”,打開“選項”面板。選擇“郵件設置”並點擊“電子郵件賬戶”,打開“賬號設置”面板。在“電子郵件”選項卡下新建郵件賬戶


五、案例[搭建James郵件服務器]

需求說明:

在本機搭建James郵件服務器,自定義服務器的名稱。

創建兩個測試用戶。

在Microsoft Outlook中配置其中一個測試用戶為Outlook郵件賬戶


 六、使用JavaMail發送電子郵件(案例)

 需求:

使用JavaMail技術,實現從A賬戶給B賬戶發送一封電子郵件,標題為“會議通知”,郵件內容為“XX你好!請於明天下午16:00 准時到B01會議室召開技術討論會。”通過Outlook 客戶端查看郵件程序發送的郵件是否發送成功

關鍵代碼:

創建一個類EmailAuthenticator並繼承自Authenticator,並植入用戶名和密碼

 

創建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
public  class  Mail {
   private  String mailServer,from,to,mailSubject,mailContent;
   private  String username,password;
   public  Mail(){
       //設置郵件信息
       //進行認證登錄的用戶名
       username= "hq@mail.com" ;
       //認證密碼
       password= "hq" ;
       //認證的郵箱對應的郵件服務器
       mailServer= "192.168.17.176" ;
       //發件人信息
       from= "wj" ;
       //收件人信息
       to= "wj@mail.com" ;
       //郵件標題
       mailSubject= "我們都是好孩子333" ;
       //郵件內容
       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協議的
       prop.put( "mail.smtp.port" "25" );
       //產生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));  //發件人
            message.setRecipient(Message.RecipientType.TO,  new  InternetAddress(to)); //收件人
            message.setSubject(mailSubject);
            //設置內容(設置字符集處理亂碼問題)
            message.setContent(mailContent, "text/html;charset=gbk" );
            message.setSentDate( new  Date());
            //創建Transport實例,發送郵件
            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
public  class  MyTest {
     public  static  void  main(String[] args) {
         Mail mail= new  Mail();
         mail.send();
         System.out.println( "success!" );
     }
 
}

 

  


七、發送帶附件的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
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.setFrom( "hq@mail.com" );
         helper.setTo( "wj@mail.com" );
         
         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
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:大配置  

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM