集册 Java实例教程 发送电子邮件格式HTML

发送电子邮件格式HTML

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

719
发送电子邮件格式HTML


import java.util.Properties;
/*
N o w J a v a . c o m 提供
*/

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.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;
/*
时   代    Java - nowjava.com
*/

import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;


public class Main{

    private final static Logger logger = Logger

            .getLogger(EmailSmtpUtil.class.getName());

    private final static String MAIL_PORT = "";

    private final static String MAIL_SERVER = "";

    private final static String MAIL_FROM = "";

    private final static String MAIL_STARTTLS = "";

    private final static String MAIL_AUTH = "";

    private final static String MAIL_USER = "";

    private final static String MAIL_PWD = "";

    public static void sendEmailFormatHtml(String to, String bodyHtml,

            String subject) {


        logger.info("sendEmailFormatHtml");

        logger.info("Message send in progress...");


        Properties properties = System.getProperties();

        properties.setProperty("mail.smtp.host", MAIL_SERVER);

        properties.setProperty("mail.smtp.port", MAIL_PORT);

        Session session = Session.getDefaultInstance(properties);

        if (MAIL_AUTH.equals("true")) {

            properties.setProperty("mail.smtp.auth", MAIL_AUTH);

            properties.setProperty("mail.smtp.starttls.enable",

                    MAIL_STARTTLS);

            session = Session.getInstance(properties,

                    new javax.mail.Authenticator() {

                        protected PasswordAuthentication getPasswordAuthentication() {

                            return new PasswordAuthentication(MAIL_USER,

                                    MAIL_PWD);

                        }

                    });

        }


        try {

            MimeMessage message = 
展开阅读全文