利用springframework+javax.mail發郵件(普通郵件、帶附件郵件、HTML格式郵件)


         Spring提供了發送電子郵件的支持,可以發送普通郵件、帶附件郵件、HTML格式郵件,甚至還可以使用Velocity模板定制化郵件內容。

一、引入相關的庫

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
<!-- spring核心庫 -->
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-core</ artifactId >
             < version >4.2.5.RELEASE</ version >
         </ dependency >
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-beans</ artifactId >
             < version >4.2.5.RELEASE</ version >
         </ dependency >
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-context</ artifactId >
             < version >4.2.5.RELEASE</ version >
         </ dependency >
 
         <!--發送Email-->
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-context-support</ artifactId >
             < version >4.2.5.RELEASE</ version >
         </ dependency >
         < dependency >
             < groupId >javax.mail</ groupId >
             < artifactId >mail</ artifactId >
             < version >1.4.7</ version >
         </ dependency >


二、發送郵件代碼

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
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
 
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
 
public class SpringEmail {
 
     private JavaMailSenderImpl mailSender= null ;
     //郵件用戶名
     private String userName= "" ;
     //發送郵箱名稱
     private String from= "@163.com" ;
     //接收郵箱名稱
     private String to= "@qq.com" ;
 
     public SpringEmail()
     {
         this .mailSender = new JavaMailSenderImpl();
         //郵箱smtp服務器
         mailSender.setHost( "smtp.163.com" );
         mailSender.setPort( 25 );
         mailSender.setUsername( this .userName);
         //郵箱密碼
         mailSender.setPassword( "xxxx" );
     }
 
     //普通文本Email
     public void sendPureMail() {
         SimpleMailMessage message = new SimpleMailMessage();
 
         String spitterName = "這里是標題(純文本)" ;
         message.setFrom( this .from);
         message.setTo( this .to);
         message.setSubject( "這里是標題!" );
         message.setText( "這里是內容" );
         this .mailSender.send(message);
     }
 
     //帶多個附件的Email
     public void sendMailWithAttachment() throws MessagingException {
 
         MimeMessage message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message, true );
 
         helper.setFrom( this .from);
         helper.setTo( this .to);
         helper.setSubject( "這里是標題(帶多個附件)!" );
         helper.setText( "這里是內容(帶附件)" );
 
         //添加兩個附件(附件位置位於java-->resources目錄),可根據需要添加或修改
         ClassPathResource image = new ClassPathResource( "/coupon.jpg" );
         ClassPathResource PDF = new ClassPathResource( "/Rop Reference.pdf" );
         helper.addAttachment( "Coupon.png" , image);
         helper.addAttachment( "Rop Reference.pdf" , PDF);
 
         this .mailSender.send(message);
     }
 
     //帶附件的HTML格式的Email
     public void sendMailHtmlWithAttachment() throws MessagingException {
 
         MimeMessage message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message, true , "GBK" ); //解決亂碼問題
 
         helper.setFrom( this .from);
         helper.setTo( this .to);
         helper.setSubject( "這里是標題(Html帶附件)!" );
         //設置META解決亂碼問題
         helper.setText( "<html><META http-equiv=Content-Type content='text/html; charset=GBK'><body><img src='cid:Coupon'>" +
                 "<h4>" + "測試亂碼" + " says...</h4>" +
                 "<i>" + "測試亂碼2" + "</i>" +
                 "</body></html>" , true );
 
         //圖片嵌入到html文件中
         ClassPathResource couponImage = new ClassPathResource( "/coupon.jpg" );
         helper.addInline( "Coupon" , couponImage);
 
         //圖片作為附件發送
         ClassPathResource couponImage2 = new ClassPathResource( "/coupon.jpg" );
         helper.addAttachment( "Coupon.png" , couponImage2);
 
 
         this .mailSender.send(message);
     }
 
     public static void main(String[] args) throws MessagingException{
         System.out.println( "開始發送郵件" );
 
         SpringEmail email= new SpringEmail();
         //email.sendPureMail();
         //email.sendMailWithAttachment();
         email.sendMailHtmlWithAttachment();
 
         System.out.println( "郵件發送成功" );
     }
}







免責聲明!

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



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