使用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
老妖 閱讀(2592)
評論(2) 編輯 收藏