simple java mail
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 }
标题和内容尽量用正常的内容,否则会被标识为垃圾邮件,被系统退回,附件越大,发送的时间越长