简体   繁体   中英

Sending an email in HTML format

I would like to send an email with HTML table format.

I am trying to send an email in HTML format. It's working fine when I run through Eclipse IDE . But when I try to run through Apache Tomcat Directory (put the project inside WebApp folder and then run the server) then it send the email which shows the html code, it doesn't shows the rich text itself.

Java Code:

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

Actual Output: (which comes wrong when running through Tomcat Directory)

<!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>

Expected Output:(which comes fine when running through Eclipse IDE)

See the below image:

You have a typo in you're mime type. Should read text/html not test/html in the line message.setContent(...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM