上一篇講了使用JavaMail發送普通郵件(【Mail】JavaMail介紹及發送郵件(一)),本例講發送復雜的郵件(帶有附件的郵件)
生成一封復雜的郵件
- 新建一個JavaWeb的Maven工程,引入javamail.jar包,maven引用如下:
<!-- javamail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
2 .新建郵件的結構圖,以及郵件MIME關系圖如下
3.新建一個Demo3.java,如下:
package com.hd.javamail; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import com.sun.mail.util.MimeUtil; /** * * @author H__D * @date 2016年12月6日 下午7:38:33 * */ public class Demo3 { /** * 生成一份本地的郵件 * @param args * @throws MessagingException * @throws IOException */ public static void main(String[] args) throws MessagingException, IOException { //環境 Session session = Session.getDefaultInstance(new Properties()); //郵件 MimeMessage msg = new MimeMessage(session); //設置主題 msg.setSubject("test123456"); //發件人,注意中文的處理 msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText( "某某") + "\"<xxxxxx@163.com>")); //設置郵件回復人 msg.setReplyTo(new Address[]{new InternetAddress("123456@163.com")}); //整封郵件的MINE消息體 MimeMultipart msgMultipart = new MimeMultipart("mixed");//混合的組合關系 //設置郵件的MINE消息體 msg.setContent(msgMultipart); //附件1 MimeBodyPart attch1 = new MimeBodyPart(); //附件2 MimeBodyPart attch2 = new MimeBodyPart(); //正文內容 MimeBodyPart content = new MimeBodyPart(); //把內容,附件1,附件2加入到 MINE消息體中 msgMultipart.addBodyPart(attch1); msgMultipart.addBodyPart(attch2); msgMultipart.addBodyPart(content); //把文件,添加到附件1中 //數據源 DataSource ds1 = new FileDataSource(new File("C:/Users/H__D/Desktop/1.txt")); //數據處理器 DataHandler dh1 = new DataHandler(ds1 ); //設置第一個附件的數據 attch1.setDataHandler(dh1); //設置第一個附件的文件名 attch1.setFileName("file1.jpg"); //把文件,添加到附件2中 DataSource ds2 = new FileDataSource(new File("C:/Users/H__D/Desktop/2.txt")); DataHandler dh2 = new DataHandler(ds2 ); attch2.setDataHandler(dh2); attch2.setFileName(MimeUtility.encodeText( "文件2.jpg")); //正文(圖片和文字部分) MimeMultipart bodyMultipart = new MimeMultipart("related"); //設置內容為正文 content.setContent(bodyMultipart); //html代碼部分 MimeBodyPart htmlPart = new MimeBodyPart(); //html中嵌套的圖片部分 MimeBodyPart imgPart = new MimeBodyPart(); //正文添加圖片和html代碼 bodyMultipart.addBodyPart(htmlPart); bodyMultipart.addBodyPart(imgPart); //把文件,添加到圖片中 DataSource imgds = new FileDataSource(new File("C:/Users/H__D/Desktop/logo.png")); DataHandler imgdh = new DataHandler(imgds ); imgPart.setDataHandler(imgdh); //說明html中的img標簽的src,引用的是此圖片 imgPart.setHeader("Content-Location", "http://sunteam.cc/logo.jsg"); //html代碼 htmlPart.setContent("<span style='color:red'>中文呵呵</span><img src=\"http://sunteam.cc/logo.jsg\">","text/html;charset=utf-8"); //生成文件郵件 msg.saveChanges(); //輸出 OutputStream os = new FileOutputStream("C:/Users/H__D/Desktop/demo.eml"); msg.writeTo(os); os.close(); } }
4. 運行main方法,等到郵件文件。可以使用記事本和Foxmail打開效果如下:
- 記事本效果:
Foxmai效果:
發送本地郵件文件
- 新建一個Demo4.類,如下:
package com.hd.javamail; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * * @author H__D * @date 2016年12月6日 下午7:51:43 * */ public class Demo4 { public static void main(String[] args) throws FileNotFoundException, MessagingException { // 屬性對象 Properties properties = new Properties(); // 開啟debug調試 ,打印信息 properties.setProperty("mail.debug", "true"); // 發送服務器需要身份驗證 properties.setProperty("mail.smtp.auth", "true"); // 發送服務器端口,可以不設置,默認是25 properties.setProperty("mail.smtp.port", "25"); // 發送郵件協議名稱 properties.setProperty("mail.transport.protocol", "smtp"); // 設置郵件服務器主機名 properties.setProperty("mail.host", "smtp.163.com"); // 環境信息 Session session = Session.getInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 在session中設置賬戶信息,Transport發送郵件時會使用 return new PasswordAuthentication( "xxxx@163.com", "xxxxx"); } }); //讀取本地郵件 Message message = new MimeMessage(session, new FileInputStream(new File("C:/Users/H__D/Desktop/demo.eml"))); //發送郵件 Transport.send(message, InternetAddress.parse("123456@qq.cn") ); } }
此文來源於: https://www.cnblogs.com/h--d/p/6138900.html