使用Spring的javamail注意的中文问题

Posted shaokun305

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Spring的javamail注意的中文问题相关的知识,希望对你有一定的参考价值。

使用 Spring 的封装的MailSender,确实省事多了!
但在使用中,还是得注意些问题

1.增加验证,否则会报:553 authentication is required 的错误信息
Properties prop = new Properties();
prop.setProperty("mail.smtp.auth", "true");
JavaMailSenderImpl.set JavaMailProperties(prop);

2.当邮件内容是html语言时的中文问题:
初始化MimeMessageHelper辅助类时,设置"GBK" encoding!
如:
MimeMessageHelper messageHelp = new MimeMessageHelper(message,true,"GBK");
同时在设置:<META http-equiv=Content-Type content='text/html; charset=GBK'>
如果都设置为"UTF-8",在某些邮件客户端标题是冷僻字时,不能正常显示!

3.邮件附件的中文问题!
spring的文档里面说MimeMessageHelper设置了encoding,同时对title,text,attach产生作用,但还是会出问题:
解决方法:MimeUtility.encodeWord(file.getName());就OK了!

以下是可以运行的代码
SendMailDemo.java
Java代码 复制代码
  1.   
  2. import java.io.File;   
  3. import java.io.UnsupportedEncodingException;   
  4. import java.util.Properties;   
  5.   
  6. import javax.mail.MessagingException;   
  7. import javax.mail.internet.MimeMessage;   
  8. import javax.mail.internet.MimeUtility;   
  9.   
  10. import org.springframework.mail.javamail.JavaMailSenderImpl;   
  11. import org.springframework.mail.javamail.MimeMessageHelper;   
  12.   
  13. public class SendMailDemo {   
  14.     public  void send163ByMutil() throws MessagingException {   
  15.         JavaMailSenderImpl javaMail = new JavaMailSenderImpl();   
  16.         javaMail.setHost("smtp.163.com");   
  17.         javaMail.setPassword("passowrd");   
  18.         javaMail.setUsername("username");   
  19.         Properties prop = new Properties();   
  20.         prop.setProperty("mail.smtp.auth""true");        
  21.         javaMail.setJavaMailProperties(prop);   
  22.         MimeMessage message = javaMail.createMimeMessage();   
  23.         MimeMessageHelper messageHelp = new MimeMessageHelper(message,true,"GBK");   
  24.         messageHelp.setFrom("likun35@163.com");   
  25.         messageHelp.setTo("likun35@163.com");   
  26.         messageHelp.setSubject("邮件测试");   
  27.         // messageHelp   
  28.         String body = "<html><head><META http-equiv=Content-Type content='text/html; charset=GBK'></HEAD><title>test</title></head><body>dear 小燕子 /n ";   
  29.         body+="<red>This is Text!</red> pic <img src='cid:a'></img><br>hello<img src='cid:b'></img></body></html>";   
  30.         messageHelp.setText(body, true);   
  31.         messageHelp.addInline("a"new File("E:/xiezi.jpg"));   
  32.         messageHelp.addInline("b"new File("E:/logo.png"));           
  33.         File file=new File("E:/Favorites中文文件.rar");        
  34.         try {   
  35.             messageHelp.addAttachment(MimeUtility.encodeWord(file.getName()), file);   
  36.         } catch (UnsupportedEncodingException e) {             
  37.             e.printStackTrace();   
  38.         }   
  39.         javaMail.send(message);   
  40.     }   
  41.     public static void main(String[] args) throws MessagingException {         
  42.         SendMail send=new SendMail();   
  43.         send.send163ByMutil();   
  44.   
  45.     }   
  46. }  
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class SendMailDemo {
	public  void send163ByMutil() throws MessagingException {
		JavaMailSenderImpl javaMail = new JavaMailSenderImpl();
		javaMail.setHost("smtp.163.com");
		javaMail.setPassword("passowrd");
		javaMail.setUsername("username");
		Properties prop = new Properties();
		prop.setProperty("mail.smtp.auth", "true");		
		javaMail.setJavaMailProperties(prop);
		MimeMessage message = javaMail.createMimeMessage();
		MimeMessageHelper messageHelp = new MimeMessageHelper(message,true,"GBK");
		messageHelp.setFrom("likun35@163.com");
		messageHelp.setTo("likun35@163.com");
		messageHelp.setSubject("邮件测试");
		// messageHelp
		String body = "<html><head><META http-equiv=Content-Type content='text/html; charset=GBK'></HEAD><title>test</title></head><body>dear 小燕子 /n ";
		body+="<red>This is Text!</red> pic <img src='cid:a'></img><br>hello<img src='cid:b'></img></body></html>";
		messageHelp.setText(body, true);
		messageHelp.addInline("a", new File("E:/xiezi.jpg"));
		messageHelp.addInline("b", new File("E:/logo.png"));		
		File file=new File("E:/Favorites中文文件.rar");		
		try {
			messageHelp.addAttachment(MimeUtility.encodeWord(file.getName()), file);
		} catch (UnsupportedEncodingException e) {			
			e.printStackTrace();
		}
		javaMail.send(message);
	}
	public static void main(String[] args) throws MessagingException {		
		SendMail send=new SendMail();
		send.send163ByMutil();

	}
}
 

以上是关于使用Spring的javamail注意的中文问题的主要内容,如果未能解决你的问题,请参考以下文章

Spring JavaMail发送邮件

Spring JavaMail发送邮件

Spring与JavaMail

JavaMail邮件开发

spring集成的javamail出现错误

Spring JavaMail发送邮件