以HTML格式发送电子邮件

时间:2015-02-13 13:30:59

标签: java eclipse tomcat smtp

我想发送一个HTML表格格式的电子邮件。

我正在尝试以HTML格式发送电子邮件。当我运行 Eclipse IDE 时,它工作正常。但是,当我尝试运行 Apache Tomcat目录(将项目放在WebApp文件夹中,然后运行服务器)然后它发送显示html代码的电子邮件,它不会显示富文本本身

Java代码:

MimeMessage message = new MimeMessage(session);

                        String mail = (String) recipientMails.nextElement();
                        // System.out.println(mail);

                        message.addRecipient(Message.RecipientType.TO, new InternetAddress(mail));

                        message.setSubject(subject + " -: " + submission.getSubmissionName());

                        message.setContent(htmTemp + "<tr><td>" + submission.getSubmissionName() + "</td><td>" + submission.getStatus() + "</td><td>" + messg + "</td></tr>" + "</table></body></html>", "test/html");

                        logger.error(subject + "SendTo:" + recipientMail + "\n" + messg);

                        Transport.send(message);

实际输出:(运行Tomcat目录时出错)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style>
table,th,td {
    border: 1px solid black;
    border-collapse: collapse;
}

th,td {
    padding: 5px;
}
</style>![enter image description here][1]
</head>
<body>
    <table style="width: 100%">
        <tr>
            <th>Submission Name</th>
            <th>Status</th>
            <th>Details</th>
        </tr><tr><td>ELQ_1423749204038</td><td>Sent For Translation</td><td>Your submission has been sent successfully for translation.</td></tr></table></body></html>

预期输出:(通过Eclipse IDE运行时可以正常运行)

见下图:

1 个答案:

答案 0 :(得分:1)

你有一个错误,你是哑剧型。应该在行text/html

中阅读test/html而不是message.setContent(...