With commons-email, javax.mail send messages, attachments Chinese name garbled.

import java.io.UnsupportedEncodingException;
 
import javax.mail.internet.MimeUtility;
 
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
 
public class MailTest {
    public static void sendMultiPartEmail() throws EmailException {
        Attachment EmailAttachment = new new EmailAttachment ();
         // Set the attachments path 
        attachment.setPath ( "C: /test.log" );
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        // 附件描述
        attachment.setDescription("This is Smile picture");
        FileName String = "Chinese name of the attachment .log" ;
         the try {
            fileName = MimeUtility.encodeText (fileName); // solve Chinese distortion attachment 
        } the catch (UnsupportedEncodingException E) {
            e.printStackTrace ();
        }
        attachment.setName(fileName);
        // create an attachment containing Email 
        MultiPartEmail multipartemail = new new MultiPartEmail ();
        multipartemail.setHostName ( "smtp.qq.com"); // Set the host name 
        multipartemail.setCharset ( "GBK"); // Set the character encoding 
        multipartemail.addTo ( "Your QQ number qq.com @"); // set the destination address to send mail
         // set the source address and display name, here is his own issue, you can fill in any e-mail address 
        multipartemail.setFrom ( "your QQ number @ qq.com", "Chinese name displayed in the mail " );
        multipartemail.setAuthentication ( "your QQ number, excluding @ qq.com", "your QQ password"); // set the username and password 
        multipartemail.setSubject ( "test send attachments"); // set the theme 
        multipartemail.setMsg ( "no"); // set the message content 
        multipartemail.attach (attachment); // Add attachments 
        
        multipartemail.send (); // send a message 
    }
 
    public static void main(String[] args) {
        try {
            MailTest.sendMultiPartEmail();
        } catch (EmailException e) {
            e.printStackTrace ();
        }
        System.out.println("send over");
    }
}
 

With commons-email, javax.mail send messages, attachments Chinese name garbled.

 

The original need to switch MimeUtility.encodeText (fileNameString) what you can not garbled, remember this.

 

Full lower attachment sent messages containing attachments.

Guess you like

Origin www.cnblogs.com/bevis-byf/p/11760778.html