Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
在一封电子邮件中同时包含HTML和plain text格式

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

在一封电子邮件中同时包含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.

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 A
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 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);           
            msg.setType("plain/text");
            msg.setContent(mp);
            Transport.send(msg);
        } catch(Throwable t) {
            t.printStackTrace();
        }
    }

}


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 javadoc for javax.mail.

Listing B
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 = "<h2>This is a test of the good times broadcasting system</h2>";

        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();
        }
    }

}




相关文章

相关软件