In the last article( Java Mail Development (2): Send an email with graphics and text and attachments using JMail (below) ) In JavaMail, we learned how to send a complex email.
There are two remaining problems:
1. The appendix name can only be in English and Chinese.
2. The display of friendly names.
When we use 163 mailboxes to send mail, we often see Zhang Yida <zyh5540@163.com> in the recipient column. This approach was not used in the previous version of the code.
Here's how to solve these two problems
1. For Chinese scrambling, because the mail requires all characters to be ascII characters, Chinese characters of course can not. So we just need to transcode all of our Chinese.
JavaMail provides such a class: MimeUtility. The encodeText method of this class can be used to encode Chinese.
2. To display friendly names, we first need to understand the format when the recipient fills in. Generally, the format is: friendly name < mailbox address >, if there are more than one recipient, it should be separated by commas.
For example, Zhang Yida Sohu <zyh5540@sohu.com>, Zhang Yida qqq <554077931@qqq.com>, and Zhang Yida 163 <zyh5540@163.com>.
The sample code is as follows
-
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;
-
-
-
-
-
-
-
-
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);
-
-
-
msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("Zhang Yida sss") + "\" <zyh5540@163.com>"));
-
msg.setReplyTo(new Address[]{new InternetAddress("zyh5540@163.com")});
-
-
-
msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("Zhang Yida sohu") + " <zyh5540@sohu.com>,"
-
+ MimeUtility.encodeText("Zhang Yida qq") + " <554077931@qq.com>,"
-
+ MimeUtility.encodeText("Zhang Yida sina") + " <zyh5540@sina.com>,"
-
+ MimeUtility.encodeText("Zhang Yida 163") + " <zyh5540@163.com>"));
-
msg.setSubject("From 163 This is a complicated email.");
-
-
-
Multipart msgPart = new MimeMultipart("mixed");
-
msg.setContent(msgPart);
-
-
MimeBodyPart body = new MimeBodyPart();
-
MimeBodyPart attach1 = new MimeBodyPart();
-
MimeBodyPart attach2 = new MimeBodyPart();
-
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("Head portrait.jpg"));
-
-
content.setContent("<div style='color:red;font-size:18px;'>E-mail from 163</div>: I have a picture here.<img src='cid:touxiang.jpg' alt='touxiang' width=\"100px\" height='100px' />,Is it pretty?", "text/html;charset=utf-8");
-
-
-
-
attach1.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\firefox.txt")));
-
attach1.setFileName(MimeUtility.encodeText("Document 1.txt"));
-
attach2.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\java.txt")));
-
attach2.setFileName(MimeUtility.encodeText("file Document 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());
-
}
-
}
Attachment:
1.JavaMail development jar package download address: http://download.csdn.net/download/zyh5540/6900667
2. Reference Code Download Address:
http://download.csdn.net/download/zyh5540/6907731