Java email Development (III): Fix garbled attachments and display friendly names

Source: Internet
Author: User

In the previous article (Java mail Development (II): Using JMail to send an email with illustrated text and attachments (below), we learned to use JavaMail to send a complex email.

There are two issues:

1. The attachment name can only be in English and garbled Chinese Characters

2. Display friendly names.

When we use a mailbox such as 163 to send an email, we often see the following in the recipient column: Zhang Yida This method. This method is not used in the code of the previous version.

The following two problems are solved:

1. Chinese garbled characters are required because all characters in the email must be ascII characters. Chinese characters are not allowed. Therefore, we only need to transcode all our Chinese characters.

JavaMail provides the MimeUtility class. Use the encodeText method of this class to encode Chinese characters.

2. for friendly display names, we must first understand the format when the recipient is filled in. The format is generally: friendly name <邮箱地址> If multiple recipients are separated by commas (,),

For example, Zhang Yida sohu , Zhang Yida qq <554077931@qq.com>, Zhang Yi up to 163

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;/*** create a complex email, this email contains a body and two attachments *. The body must contain an image * @ 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);/* set the mail header * // use MimeUtility. the encodeText () method is used to encode the Chinese (base64 or QP) msg. setFrom (new InternetAddress ("\" "+ MimeUtility. encodeText ("Zhang Yida sss") + "\"
 
  
"); Msg. setReplyTo (new Address [] {new InternetAddress ("zyh5540@163.com")}); // do not forget a space between the friendly name and the email Address, email addresses are separated by commas. // For example, Zhang Yida sohu
  
   
, Zhang Yida qq <554077931@qq.com>, Zhang Yi up to 163
   
    
Msg. setRecipients (RecipientType. TO, InternetAddress. parse (MimeUtility. encodeText ("Zhang Yida sohu") +"
    
     
, "+ MimeUtility. encodeText (" Zhang Yida qq ") +" <554077931@qq.com>, "+ MimeUtility. encodeText (" Zhang Yida sina ") +"
     
      
, "+ MimeUtility. encodeText (" Zhang Yida 163 ") +"
      
        "); Msg. setSubject ("From 163 this is a complex email");/* set the message content, including two attachments and a piece of body */Multipart msgPart = new MimeMultipart ("mixed"); msg. setContent (msgPart); MimeBodyPart body = new MimeBodyPart (); // indicates the body MimeBodyPart attach1 = new MimeBodyPart (); // indicates Attachment 1 MimeBodyPart attach2 = new MimeBodyPart (); // indicates the attachment 2msgPart. addBodyPart (body); msgPart. addBodyPart (attach1); msgPart. addBodyPart (attach2);/* set the body * // * the body is a mix of text and image */Multipart contentPart = new MimeMultipart ("related"); body. setContent (contentPart); MimeBodyPart content = new MimeBodyPart (); // text MimeBodyPart img = new MimeBodyPart (); // picture 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 ","
       
         "); Img. setFileName (MimeUtility. encodeText (".jpg"); // sets the text content. setContent ("email sent from 163: I have an image here. Is it nice? "," Text/html; charset = UTF-8 ");/* end of body Content Setting * // set the attachment */attach1.setDataHandler (new DataHandler (new FileDataSource (" E: \ others \ firefox.txt "); attach1.setFileName (MimeUtility. encodeText ("file 1.txt"); attach2.setDataHandler (new DataHandler (new FileDataSource ("E: \ others \ java.txt"); attach2.setFileName (MimeUtility. encodeText ("filefile 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 ());}}
       
      
     
    
   
  
 

Appendix:

1. JavaMail jar package: http://download.csdn.net/download/zyh5540/6900667

2. References: http://download.csdn.net/download/zyh5540/6907731

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.