html格式邮件可以包含,在一封电子邮件中同时包含HTML和plaintext格式

在一封电子邮件中同时包含HTML和plaintext格式 (2007-04-05 23:37:29) 在一封电子邮件中同时包含HTML和plain text

a4c26d1e5885305701be709a3d33442f.png

在一封电子邮件中同时包含HTML和plaintext格式

(2007-04-05 23:37:29)

在一封电子邮件中同时包含HTML和plain text格式

The ability to promote products directly to a targeted consumer is a marketing department's dream. And e-mail marketing has helped make this type of focused promotion much easier. If you send your messages as HTML mail, you can boost their impact by including pictures and polished formatting. However, not all recipients want to read HTML; some prefer text.

One solution might be to ask your customers what type of e-mail they prefer. But there is another, more efficient solution: Include both formats in one e-mail. To do this, you need the javax.mail package that's part of J2EE and available at java.sun.com/">java.sun.com.As you can see in Listing A, sending a simple text e-mail message with Java is pretty easy. Notice, the javax mail package is imported.Listing Aa4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

import java.util.Properties;import javax.mail.*;import javax.mail.internet.*;public class Mail {static public void main(String[] args) { String to = "amxh@21cn.com"; String subj = "Testing Email"; String body = "This is a test of the good times broadcasting[b] system"; Properties props = new Properties(); // supply the location of a props.put("mail.smtp.host", "10.10.10.139"); try { Session session = Session.getDefaultInstance(props, null); Message msg = new MimeMessage(session); session.setDebug(true); msg.setFrom(new InternetAddress("amxh@21cn.com")); msg.addRecipient( Message.RecipientType.TO, newInternetAddress(to)); msg.setSubject(subj); msg.setType("plain/text"); msg.setContent(mp); Transport.send(msg); } catch(Throwable t) { t.printStackTrace(); } }}[/b]To send multiple formats in one e-mail, you have to use Parts. An e-mail has a Content, which can be a simple String or a Multipart. A Multipart is made up of many BodyParts. A BodyPart may contain a String or a Multipart. For this example, we're sending e-mails that contain Multipart, with two BodyParts—plaintext and HTML.Note that there are different types of Multipart. The two most common are multipart/mixed and multipart/alternative. One shows both on the same page, while the other selects the best format for the user. Listing B shows an example of multipart/alternative. For more information on Multiparts, check the online java.sun.com/j2ee/j2sdkee/techdocs/api/">javadoc for javax.mail.

Listing B

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

import java.util.Properties;import javax.mail.*;import javax.mail.internet.*;public class Mail { static public void main(String[] args) { String to = "amxh@21cn.com"; String subj = "net_lover"; String body = "This is a test of the good times broadcasting system"; String htmlbody = "

This is a test of the good times broadcasting system

"; Properties props = new Properties(); // supply the location of a props.put("mail.smtp.host", "10.10.10.139"); try { Session session = Session.getDefaultInstance(props, null); Message msg = new MimeMessage(session); session.setDebug(true); msg.setFrom(new InternetAddress("amxh@21cn.com")); msg.addRecipient( Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subj); MimeMultipart mp = new MimeMultipart(); BodyPart tp = new MimeBodyPart(); tp.setText(body); mp.addBodyPart(tp); tp = new MimeBodyPart(); tp.setContent(htmlbody, "text/html"); mp.addBodyPart(tp); mp.setSubType("alternative"); msg.setContent(mp); Transport.send(msg); } catch(Throwable t) { t.printStackTrace(); } }}

文章引用自:http://locoy.kalvin.cn

分享:

a4c26d1e5885305701be709a3d33442f.png喜欢

0

a4c26d1e5885305701be709a3d33442f.png赠金笔

加载中,请稍候......

评论加载中,请稍候...

发评论

登录名: 密码: 找回密码 注册记住登录状态

昵   称:

评论并转载此博文

a4c26d1e5885305701be709a3d33442f.png

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。