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 }
標題和內容儘可能用正常的內容,不然會被標識爲垃圾郵件,被系統退回,附件越大,發送的時間越長java