使用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();

	}
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值