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