集册 Java实例教程 发送简单HTML电子邮件的实用方法

发送简单HTML电子邮件的实用方法

欢马劈雪     最近更新时间:2020-01-02 10:19:05

484
发送简单HTML电子邮件的实用方法


import java.io.UnsupportedEncodingException;

import java.util.Date;
/*来自 
 时 代 J a v a 公 众 号 - nowjava.com*/

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;/**来自 nowjava.com - 时  代  Java**/

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;


public class Main{

    /**

     * Utility method to send simple HTML email

     *

     * @param session

     * @param toEmail

     * @param subject

     * @param body

     */

    public static void sendEmail(Session session, String toEmail,

            String subject, String body) {

        try {

            MimeMessage msg = new MimeMessage(session);

            //set message headers

            msg.addHeader("Content-type", "text/HTML; charset=UTF-8");

            msg.addHeader("format", "flowed");

            msg.addHeader("Content-Transfer-Encoding", "8bit");

            msg.setFrom(new InternetAddress(Config.SURVEYER_EMAIL,

                    Config.SURVEYER_NAME));

            msg.setSubject(subject, "UTF-8");

          
展开阅读全文