Java郵件開發(三):解決附件名爲亂碼及顯示友好名稱

1.附件的名稱只能爲英文,中文亂碼html

2.友好名稱的顯示。java

咱們使用163等郵箱發送郵件時,咱們常常能夠看到收件人一欄中會是:張益達 <zyh5540@163.com>這種方式。在上一版本的代碼中並無使用這樣的方式。session

下面解決這兩個問題編碼

1. 對於中文亂碼,是由於郵件要求全部的字符必須是ascII字符,中文字符固然不能夠。所以咱們只須要把咱們的中文所有轉碼就能夠了。.net

而JavaMail提供了這樣一個類:MimeUtility。使用這個類的encodeText方法就能夠對中文進行編碼。firefox

2. 對於顯示友好名稱,咱們首先要了解收件人填寫時的格式。格式通常爲:友好名稱 <郵箱地址>,若是有多個收件人要以逗號分割code

如:張益達sohu <zyh5540@sohu.com>,張益達qq <554077931@qq.com>,張益達163 <zyh5540@163.com>htm

package com.zyh.demo;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
 
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Address;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
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 javax.mail.util.ByteArrayDataSource;
 
/**
 * 建立一個複雜的郵件,這個郵件包括一段正文和兩個附件
 * 其中正文要包含一張圖片
 * @author Administrator
 *
 */
public class Demo4 {
	
	public static void main(String[] args) throws Exception {
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		
		Session session = Session.getInstance(props);
		session.setDebug(true);
		
		Message msg = new MimeMessage(session);
		/*郵件消息頭設置*/
		//使用MimeUtility.encodeText()方法對中文進行編碼(base64或QP)
		msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("張益達sss") + "\" <zyh5540@163.com>"));
		msg.setReplyTo(new Address[]{new InternetAddress("zyh5540@163.com")});
		//友好名稱和郵箱地址之間不要忘了有一空格,郵箱地址之間用逗號分隔
		//如:張益達sohu <zyh5540@sohu.com>,張益達qq <554077931@qq.com>,張益達163 <zyh5540@163.com>
		msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("張益達sohu") + " <zyh5540@sohu.com>," 
				+ MimeUtility.encodeText("張益達qq") + " <554077931@qq.com>,"
				+ MimeUtility.encodeText("張益達sina") + " <zyh5540@sina.com>,"
				+ MimeUtility.encodeText("張益達163") + " <zyh5540@163.com>"));
		msg.setSubject("From 163 這是一封複雜郵件");
		
		/*郵件消息內容設置,包括兩個附件和一段正文*/
		Multipart msgPart = new MimeMultipart("mixed");
		msg.setContent(msgPart);		
		
		MimeBodyPart body = new MimeBodyPart();     //表示正文
		MimeBodyPart attach1 = new MimeBodyPart();  //表示附件1
		MimeBodyPart attach2 = new MimeBodyPart();  //表示附件2
		msgPart.addBodyPart(body);
		msgPart.addBodyPart(attach1);
		msgPart.addBodyPart(attach2);
		
		/*如下爲設置正文*/
		/*正文是文字和圖片混合的*/
		Multipart contentPart = new MimeMultipart("related");
		body.setContent(contentPart);
		MimeBodyPart content = new MimeBodyPart(); //文字
		MimeBodyPart img = new MimeBodyPart();     //圖片
		contentPart.addBodyPart(content);
		contentPart.addBodyPart(img);
		
		DataSource fileds = new ByteArrayDataSource(new FileInputStream("D:\\picture\\jpg\\touxiang.jpg"),"image/jpeg");  
        DataHandler imgDataHandler = new DataHandler(fileds);
		img.setDataHandler(imgDataHandler);
		img.setHeader("Content-ID", "<touxiang.jpg>");
		img.setFileName(MimeUtility.encodeText("頭像.jpg"));
		//設置文字內容
		content.setContent("<div style='color:red;font-size:18px;'>從163發來的郵件</div>:我這裏有一張圖片<img src='cid:touxiang.jpg' alt='touxiang' width=\"100px\" height='100px' />,好看嗎?", "text/html;charset=utf-8");
		/*正文內容設置結束*/
		
		/*下面爲設置附件*/
		attach1.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\firefox.txt")));
		attach1.setFileName(MimeUtility.encodeText("文件1.txt"));
		attach2.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\java.txt")));
		attach2.setFileName(MimeUtility.encodeText("file文件2.txt"));
		
		msg.saveChanges();
		
		OutputStream os = new FileOutputStream("E:\\demo4.eml");
		msg.writeTo(os);
		os.close();
		
		Transport trans = session.getTransport();
		trans.connect("smtp.163.com", 25, "zyh5540", "test");
		trans.sendMessage(msg,msg.getAllRecipients());
	}
}

  

https://blog.csdn.net/zyh5540/article/details/19019397blog

相關文章
相關標籤/搜索