使用javamail发送html邮件比较复杂
1 packageorg.tatan.mail;2 3 importjavax.mail.internet.InternetAddress;4 importjavax.mail.internet.MimeMessage;5 importjavax.mail.internet.MimeUtility;6 importjavax.mail.Session;7 importjavax.mail.MessagingException;8 importjavax.mail.Transport;9 10 publicclassSendHtmlMail {11 publicstaticvoidsendMessage(String smtpHost,12 String from, String to,13 String subject, String messageText)14 throwsMessagingException,java.io.UnsupportedEncodingException {15 16 //Step 1: Configure the mail session17 System.out.println("Configuring mail session for:"+smtpHost);18 java.util.Properties props=newjava.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 message26 System.out.println("Constructing message - from="+from+"to="+to);27 InternetAddress fromAddress=newInternetAddress(from);28 InternetAddress toAddress=newInternetAddress(to);29 30 MimeMessage testMessage=newMimeMessage(mailSession);31 testMessage.setFrom(fromAddress);32 testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);33 testMessage.setSentDate(newjava.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 message40 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 publicstaticvoidmain(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=newStringBuffer();57 theMessage.append("
这倒霉孩子
");58 theMessage.append("");59 theMessage.append(" 年年失望年年望");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这个里面的信息来对邮件正文进行解码。