Java Mail Development (3): Solve the problem of scrambling attachments and displaying friendly names

Posted by Modernvox on Sat, 08 Jun 2019 03:34:22 +0200

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

  1. package com.zyh.demo;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.OutputStream;  
  6. import java.util.Properties;  
  7.   
  8. import javax.activation.DataHandler;  
  9. import javax.activation.DataSource;  
  10. import javax.activation.FileDataSource;  
  11. import javax.mail.Message;  
  12. import javax.mail.Message.RecipientType;  
  13. import javax.mail.Address;  
  14. import javax.mail.Multipart;  
  15. import javax.mail.Session;  
  16. import javax.mail.Transport;  
  17. import javax.mail.internet.InternetAddress;  
  18. import javax.mail.internet.MimeBodyPart;  
  19. import javax.mail.internet.MimeMessage;  
  20. import javax.mail.internet.MimeMultipart;  
  21. import javax.mail.internet.MimeUtility;  
  22. import javax.mail.util.ByteArrayDataSource;  
  23.   
  24. /** 
  25.  * Create a complex message that includes a body and two attachments 
  26.  * The text should contain a picture. 
  27.  * @author Administrator 
  28.  * 
  29.  */  
  30. public class Demo4 {  
  31.       
  32.     public static void main(String[] args) throws Exception {  
  33.         Properties props = new Properties();  
  34.         props.setProperty("mail.smtp.auth""true");  
  35.         props.setProperty("mail.transport.protocol""smtp");  
  36.           
  37.         Session session = Session.getInstance(props);  
  38.         session.setDebug(true);  
  39.           
  40.         Message msg = new MimeMessage(session);  
  41.         /*Mail header settings*/  
  42.         //Using MimeUtility.encodeText() method to encode Chinese (base64 or QP)  
  43.         msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("Zhang Yida sss") + "\" <zyh5540@163.com>"));  
  44.         msg.setReplyTo(new Address[]{new InternetAddress("zyh5540@163.com")});  
  45.         //Don't forget that there is a space between friendly names and mailbox addresses. The mailbox addresses are separated by commas.  
  46.         //For example, Zhang Yida Sohu <zyh5540@sohu.com>, Zhang Yida qqq <554077931@qqq.com>, and Zhang Yida 163 <zyh5540@163.com>.  
  47.         msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("Zhang Yida sohu") + " <zyh5540@sohu.com>,"   
  48.                 + MimeUtility.encodeText("Zhang Yida qq") + " <554077931@qq.com>,"  
  49.                 + MimeUtility.encodeText("Zhang Yida sina") + " <zyh5540@sina.com>,"  
  50.                 + MimeUtility.encodeText("Zhang Yida 163") + " <zyh5540@163.com>"));  
  51.         msg.setSubject("From 163 This is a complicated email.");  
  52.           
  53.         /*Mail message content settings, including two attachments and a paragraph of text*/  
  54.         Multipart msgPart = new MimeMultipart("mixed");  
  55.         msg.setContent(msgPart);          
  56.           
  57.         MimeBodyPart body = new MimeBodyPart();     //Express text  
  58.         MimeBodyPart attach1 = new MimeBodyPart();  //Represents Annex 1  
  59.         MimeBodyPart attach2 = new MimeBodyPart();  //Representation of Annex 2  
  60.         msgPart.addBodyPart(body);  
  61.         msgPart.addBodyPart(attach1);  
  62.         msgPart.addBodyPart(attach2);  
  63.           
  64.         /*The following is to set the body*/  
  65.         /*Text is a mixture of words and pictures.*/  
  66.         Multipart contentPart = new MimeMultipart("related");  
  67.         body.setContent(contentPart);  
  68.         MimeBodyPart content = new MimeBodyPart(); //Written words  
  69.         MimeBodyPart img = new MimeBodyPart();     //picture  
  70.         contentPart.addBodyPart(content);  
  71.         contentPart.addBodyPart(img);  
  72.           
  73.         DataSource fileds = new ByteArrayDataSource(new FileInputStream("D:\\picture\\jpg\\touxiang.jpg"),"image/jpeg");    
  74.         DataHandler imgDataHandler = new DataHandler(fileds);  
  75.         img.setDataHandler(imgDataHandler);  
  76.         img.setHeader("Content-ID""<touxiang.jpg>");  
  77.         img.setFileName(MimeUtility.encodeText("Head portrait.jpg"));  
  78.         //Setting Text Content  
  79.         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");  
  80.         /*End of text content setting*/  
  81.           
  82.         /*Here are the settings for attachments*/  
  83.         attach1.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\firefox.txt")));  
  84.         attach1.setFileName(MimeUtility.encodeText("Document 1.txt"));  
  85.         attach2.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\java.txt")));  
  86.         attach2.setFileName(MimeUtility.encodeText("file Document 2.txt"));  
  87.           
  88.         msg.saveChanges();  
  89.           
  90.         OutputStream os = new FileOutputStream("E:\\demo4.eml");  
  91.         msg.writeTo(os);  
  92.         os.close();  
  93.           
  94.         Transport trans = session.getTransport();  
  95.         trans.connect("smtp.163.com"25"zyh5540""test");  
  96.         trans.sendMessage(msg,msg.getAllRecipients());  
  97.     }  
  98. }  

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

Topics: Session Java ascii Firefox