利用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("邮件发送成功");
    }
}






posted @ 2016-09-20 09:41    阅读(4408)  评论(0编辑  收藏  举报