老妖的博客
现实的中没有几个人能够真为对方去死,甚至山盟海誓很快就会在金钱面前变的微不足道,这才是生活。没有永远的爱,除了你的父母对你,当然也就没有永远的恨,更没有永远的痛,时间是最好的治疗大师,它会很快抚平你心灵上累累的伤痕。很多年以后你想起来时,那些在你生命中汹涌来往的人群至多是个模糊的影子或者毫无意义的名字
posts - 105,  comments - 171,  trackbacks - 0

使用javamail发送html邮件比较复杂

 

 1 package org.tatan.mail;
 2 
 3 import javax.mail.internet.InternetAddress;
 4 import javax.mail.internet.MimeMessage;
 5 import javax.mail.internet.MimeUtility;
 6 import javax.mail.Session;
 7 import javax.mail.MessagingException;
 8 import javax.mail.Transport;
 9 
10 public class SendHtmlMail {
11     public static void sendMessage(String smtpHost,
12                                    String from, String to,
13                                    String subject, String messageText)
14             throws MessagingException,java.io.UnsupportedEncodingException {
15 
16         // Step 1:  Configure the mail session
17         System.out.println("Configuring mail session for: " + smtpHost);
18         java.util.Properties props = new java.util.Properties();
19         props.setProperty("mail.smtp.auth""true");//指定是否需要SMTP验证
20         props.setProperty("mail.smtp.host", smtpHost);//指定SMTP服务器
21         props.put("mail.transport.protocol""smtp");
22         Session mailSession = Session.getDefaultInstance(props);
23         mailSession.setDebug(true);//是否在控制台显示debug信息
24 
25         // Step 2:  Construct the message
26         System.out.println("Constructing message -  from=" + from + "  to=" + to);
27         InternetAddress fromAddress = new InternetAddress(from);
28         InternetAddress toAddress = new InternetAddress(to);
29 
30         MimeMessage testMessage = new MimeMessage(mailSession);
31         testMessage.setFrom(fromAddress);
32         testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
33         testMessage.setSentDate(new java.util.Date());
34         testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));
35 
36         testMessage.setContent(messageText, "text/html;charset=gb2312");
37         System.out.println("Message constructed");
38 
39         // Step 3:  Now send the message
40         Transport transport = mailSession.getTransport("smtp");
41         transport.connect(smtpHost, "webmaster""password");
42         transport.sendMessage(testMessage, testMessage.getAllRecipients());
43         transport.close();
44 
45 
46         System.out.println("Message sent!");
47     }
48 
49     public static void main(String[] args) {
50 
51         String smtpHost = "localhost";
52         String from = "webmaster@mymail.com";
53         String to = "mfc42d@sohu.com";
54         String subject = "html邮件测试"//subject javamail自动转码
55 
56         StringBuffer theMessage = new StringBuffer();
57         theMessage.append("<h2><font color=red>这倒霉孩子</font></h2>");
58         theMessage.append("<hr>");
59         theMessage.append("<i>年年失望年年望</i>");
60 
61         try {
62             SendHtmlMail.sendMessage(smtpHost, from, to, subject, theMessage.toString());
63         }
64         catch (javax.mail.MessagingException exc) {
65             exc.printStackTrace();
66         }
67         catch (java.io.UnsupportedEncodingException exc) {
68             exc.printStackTrace();
69         }
70     }
71 }
72 


邮件头(参见RFC822,RFC2047)只能包含US-ASCII字符。
邮件头中任何包含非US-ASCII字符的部分必须进行编码,使其只包含US-ASCII字符。
但是java mail可以根据JVM发送中文邮件自行编码,,用它自带的MimeUtility类的encodeText方法对中文信息进行编码也可以。
邮件正文必须有charset=gb2312否则为
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
打开邮件为乱码,设置charset=gb2312后
Content-Type: text/html;charset=gb2312
Content-Transfer-Encoding: quoted-printable
它不能用MimeUtility里的方法来编码。
邮件正文的编码方式的信息是要放在Content-Transfer-Encoding这个邮件头参数中的,
而MimeUtility里面的方法是将编码方式的信息放在编码后的正文内容中。
所以如果你对正文也用MimeUtility进行处理,那么其他邮件程序就不会正常显示你编码的邮件,
因为其他邮件软件如outlook,foxmail只会根据Content-Transfer-Encoding这个里面的信息来对邮件正文进行解码。

posted on 2005-11-03 19:36 老妖 阅读(2578) 评论(2)  编辑  收藏

FeedBack:
# re: 使用javamail发送html邮件--摘自邢红瑞的blog[未登录]
2013-05-24 10:54 | 123
123  回复  更多评论
  
# re: 使用javamail发送html邮件--摘自邢红瑞的blog[未登录]
2013-05-24 10:54 | 456
456  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2005年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

随笔分类(48)

随笔档案(104)

好友链接

我的豆瓣

积分与排名

  • 积分 - 218787
  • 排名 - 256

最新评论

阅读排行榜