基於 JavaEmail 簡單的發送郵件點到點,一對多(圖片和附件)

來源:互聯網
上載者:User

標籤:mem   注意   登入   etc   AC   內容   抽象類別   add   文字   

使用JUnit4啟動並執行,根據自己喜好

話不多說直接上代碼

  1 @Test  2     public void sendEmailTest() {  3         try {  4             Properties properties = new Properties();  5             // 設定smtp主機  6             properties.put("mail.smtp.host", "smtp.exmail.qq.com");  7             // 使用smtp身分識別驗證  8             properties.put("mail.smtp.auth", "true");  9             /* 10              * 在 JavaMail 中,可以通過 extends Authenticator 抽象類別,在子類中覆蓋父類中的 11              * getPasswordAuthentication() 方法,就可以實現以不同的方式來進行登入郵箱時的使用者身份認證。JavaMail 12              * 中的這種設計是使用了策略模式(Strategy 13              */ 14             MimeMessage message = new MimeMessage(Session.getInstance(properties, new Authenticator() { 15                 @Override 16                 public PasswordAuthentication getPasswordAuthentication() { 17                     return new PasswordAuthentication(//設定發送帳號密碼 18                             "帳號", "密碼"); 19                 } 20             })); 21             // 設定郵件的屬性 22  23             // 設定郵件的寄件者 24 //            message.setFrom(new InternetAddress("發送人帳號")); 25             message.setFrom(new InternetAddress("帳號")); 26             // 設定郵件的收件者 cc表示抄送 bcc 表示暗送 27             message.setRecipient(Message.RecipientType.TO, new InternetAddress("接收人郵箱")); 28             // 設定郵件的主題 29             message.setSubject("我是郵件主題"); 30             // 建立郵件的本文 31             MimeBodyPart text = new MimeBodyPart(); 32             // setContent(“郵件的本文內容”,”設定郵件內容的編碼方式”) 33             //內容可用css直接控製圖片大小 34             String html = "<div><p>內容文字</p><div><hr style=‘border:1px dashed #e4e4e4;width:360px;‘/><img src=‘cid:a‘ style=‘width:100px;‘><h5>XXXXX有限公司</h5></div></div>"; 35             text.setContent(html, "text/html;charset=gb2312"); 36  37  38             // 一對多發送只要改一個地方如下: 39  40 //            String toMore[] = {}; 41 //            // // 構建一個群發地址數組 42 //            InternetAddress[] adr = new InternetAddress[toMore.length]; 43 //            for (int i = 0; i < toMore.length; i++) { 44 //                adr[i] = new InternetAddress(toMore[i]); 45 //            } 46             // Message的setRecipients方法支援群發。。注意:setRecipients方法是複數和點 到點不一樣 47 //            message.setRecipients(Message.RecipientType.TO, adr); 48  49  50             // 建立圖片 51             MimeBodyPart img = new MimeBodyPart(); 52             /* 53              * JavaMail API不限制資訊只為文本,任何形式的資訊都可能作繭自縛MimeMessage的一部分. 54              * 除了文本資訊,作為檔案附件包含在電子郵件資訊的一部分是很普遍的. JavaMail 55              * API通過使用DataHandler對象,提供一個允許我們包含非文本BodyPart對象的簡便方法. 56              */ 57  58 //            File file = new File("src/main/resources/321.png"); 59             //圖片路徑 60 //            DataHandler dh = new DataHandler(new FileDataSource(file)); 61             //圖片路徑 62             DataHandler dh = new DataHandler(new FileDataSource("d://123.jpg")); 63             img.setDataHandler(dh); 64             // 建立圖片的一個表示用於顯示在郵件中顯示 65             //html 中 src=‘cid:a‘ 表示引用 66             img.setContentID("a"); 67             // 關係 本文和圖片的 68             MimeMultipart mm = new MimeMultipart(); 69             mm.addBodyPart(text); 70             mm.addBodyPart(img); 71             // 設定本文與圖片之間的關係 72             mm.setSubType("related"); 73             // 圖班與本文的 body 74             MimeBodyPart all = new MimeBodyPart(); 75             all.setContent(mm); 76  77             // 附件與本文(text 和 img)的關係 78             MimeMultipart mm2 = new MimeMultipart(); 79             mm2.addBodyPart(all); 80  81  82  83             // 建立附件 84             MimeBodyPart attch = new MimeBodyPart(); 85             //附件1 86             //資料來源 87  88             File file = new File("d://123.jpg"); 89             InputStream c = new FileInputStream(file);; 90             DataSource source1 = new ByteArrayDataSource(c, "application/octet-stream"); 91             //設定第一個附件的資料 92             attch.setDataHandler(new DataHandler(source1));   //new DataHandler(source1) 資料處理器 93             String fileName = "123.jpg"; //可以是jpg,zip,excle  (rar等其他應該也是可以的,沒有具體測試) 94             // 中文亂碼問題/設定第一個附件的檔案名稱 95             attch.setFileName(MimeUtility.encodeText(fileName)); 96             mm2.addBodyPart(attch); 97  98  99 100             message.setContent(mm2);101             // 儲存修改102             message.saveChanges();103             // 發送郵件104             Transport.send(message);105 106         } catch (Exception e) {107             e.printStackTrace();108         }109     }

運行查看接收郵箱,如:

 

基於 JavaEmail 簡單的發送郵件點到點,一對多(圖片和附件)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.