码迷,mamicode.com
首页 > 编程语言 > 详细

simple java mail

时间:2019-03-17 20:04:46      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:val   test   文件   images   sub   author   zip   des   subject   

1 <dependency>
2     <groupId>org.simplejavamail</groupId>
3     <artifactId>simple-java-mail</artifactId>
4     <version>5.1.3</version>
5 </dependency>
 1 /** 
 2  *文件类型参考http://www.w3school.com.cn/media/media_mimeref.asp mime手册
 3  *@author Tele
 4  *
 5  */
 6 public class Demo {
 7     @Test
 8     public void test() throws FileNotFoundException, IOException {
 9         
10         
11         ByteArrayDataSource img1 = new ByteArrayDataSource(new FileInputStream("./src/main/resources/a.jpg"),"image/png");
12     
13         ByteArrayDataSource img2 = new ByteArrayDataSource(new FileInputStream("./src/main/resources/b.jpg"),"image/png");
14         
15         //发送多张图片
16         AttachmentResource attachmentResource1 = new AttachmentResource("image1.jpg",img1);
17         AttachmentResource attachmentResource2 = new AttachmentResource("image2.jpg",img2);
18         
19         List<AttachmentResource> list = new ArrayList<>();
20         list.add(attachmentResource1);
21         list.add(attachmentResource2);
22         
23         
24         //发送附件
25         ByteArrayDataSource zip = new ByteArrayDataSource(new FileInputStream("C:\\Users\\Administrator\\Desktop\\image.zip"),"application/zip");
26         
27         
28         Email email = EmailBuilder.startingBlank()
29                   .from("tele","账号")
30                   .to("二狗", "账号")
31                   .withSubject("hey,春游么,今天又是阳光明媚的一天啊")
32                   .withPlainText("发送图片测试")
33                   .withHeader("X-Priority", 5)
34                   .withEmbeddedImage("xx.jpg",img1)
35                   .withEmbeddedImages(list)
36               //    .withAttachment("image.zip", zip)
37                   .withReturnReceiptTo()
38                   .buildEmail();
39 
40         Mailer mailer = MailerBuilder
41                 //smtp授权码
42                   .withSMTPServer("smtp.163.com", 25, "账号", "smtp授权码")
43                   .withSessionTimeout(10 * 1000)
44            //       .clearEmailAddressCriteria() // turns off email validation
45                   .withProperty("mail.smtp.sendpartial", "true")
46            //       .withDebugLogging(true)
47                   .buildMailer();
48                  
49 
50         mailer.sendMail(email);
51     }
52     
53     
54     
55     
56     @Test
57     public void test2() throws InterruptedException, FileNotFoundException, IOException {
58         for(int i=0;i<10;i++) {
59             test();
60             Thread.sleep(2000);
61         }
62         
63     }
64     
65     
66 }

标题和内容尽量用正常的内容,否则会被标识为垃圾邮件,被系统退回,附件越大,发送的时间越长

技术图片

 

 技术图片

 

simple java mail

标签:val   test   文件   images   sub   author   zip   des   subject   

原文地址:https://www.cnblogs.com/tele-share/p/10548427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!