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

类别:Java 点击:0 评论:0 推荐:

在一封电子邮件中同时包含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 = "[email protected]";
        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("[email protected]"));
            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 = "[email protected]";
        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("[email protected]"));
            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://com.8s8s.com/it/it18127.htm