SpringMai transmits html text and attachments, solves html Chinese garbled characters, and solves Chinese name garbled characters of attachments.

Source: Internet
Author: User

Package cn. itcast. mail;

Import java. io. UnsupportedEncodingException;
Import java. util. Properties;

Import javax. mail. MessagingException;
Import javax. mail. internet. InternetAddress;
Import javax. mail. internet. MimeMessage;

Import org. springframework. core. io. ClassPathResource;
Import org. springframework. mail. javamail. JavaMailSenderImpl;
Import org. springframework. mail. javamail. MimeMessageHelper;

Import com. sun. xml. messaging. saaj. packaging. mime. internet. MimeUtility;

Public class implements pringmailtest {
/*
* Chinese Characters in html and English name of attachments are garbled.
* Some Chinese garbled characters need to be fixed during use!

1. Add verification; otherwise, an error message of 553 authentication is required will be reported.
Properties prop = new Properties ();
Prop. setProperty ("mail. smtp. auth", "true ");
JavaMailSenderImpl. setJavaMailProperties (prop );

2. Chinese when the email content is in HTML:
When initializing the MimeMessageHelper helper class, set "GBK" encoding! For example:
MimeMessageHelper messageHelp = new MimeMessageHelper (message, true, "GBK ");
At the same time, set <META http-equiv = Content-Type content = 'text/html; charset = gbk'>
If both are set to "UTF-8", the title of some mail clients cannot be displayed normally!

3. A Chinese Question about the email attachment!
In the spring document, it is said that MimeMessageHelper has set encoding and has a function on title, text, and attach, but problems still occur:
Solution: MimeUtility. encodeWord (file. getName (); OK!


*
*/
Public static void sendMail () throws MessagingException, UnsupportedEncodingException
{
JavaMailSenderImpl impl = new JavaMailSenderImpl ();
Impl. setHost ("smtp.163.com ");
 
Properties properties = new Properties ();
Properties. setProperty ("mail. smtp. auth", "true ");
Impl. setJavaMailProperties (properties );
Impl. setUsername ("ch469299503 ");
Impl. setPassword ("CH13410084766 ");
 
 
MimeMessage message = impl. createMimeMessage ();
// The third parameter is garbled
MimeMessageHelper helper = new MimeMessageHelper (message, true, "GBK ");
Helper. setSubject ("this is to send html text ");
Helper. setFrom (new InternetAddress ("ch469299503@163.com "));
Helper. setTo (new InternetAddress ("ch900915caohuan@163.com "));
// This is the html text to be sent. The second parameter indicates that the previous content is in html format. The cid: file here, And the cid above indicates that contentId is a fixed value, the following file is
// Placeholder. The following helper. addInline ("file", classPathResource) is used to fill the placeholder
/*
*
*
*/
// The head part here is used to solve the garbled characters
Helper. setText (""<Meta http-equiv = 'description' content = 'This is my page'> <meta http-equiv = 'content-type' content = 'text/html; charset = GBK '> "+
"</Head> <body> This is love </body> ClassPathResource classPathResource = new ClassPathResource ("mianshi.jpg ");
// When this Code fills in the placeholder above, it also uploads this image as an attachment
Helper. addInline ("file", classPathResource );
// Solve the problem of garbled Chinese names of attachments.
Helper. addAttachment (MimeUtility. encodeWord (" .jpg"), classPathResource );
// Impl. send (message );
}
 
Public static void main (String [] args ){
 
Try {
Try {
SendMail ();
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
} Catch (MessagingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

}

 


Author: ch469299503

Related Article

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.