在MultiPartEmail中设置HTML文本

问题描述 投票:1回答:3

我有用于发送带有附件的电子邮件的Java代码,如下所示。

String myEmailId = "[email protected]";
String myPassword = "@xx";
String senderId = "[email protected]";
try {
    MultiPartEmail email = new MultiPartEmail();
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator(myEmailId, myPassword));
    email.setDebug(true);
    email.setHostName("smtp.mail.yahoo.com");
    email.addTo(senderId);
    email.setFrom(myEmailId);
    email.setSubject("The picture");
    email.setMsg("<font face='verdana' size='3'>Here is the picture you wanted "
            + "<table>"
            + "<tr><th>id</th><th>Name</th></tr>"
            + "<tr><th>1</th><th>Name 1</th></tr>"
            + "<tr><th>2</th><th>Name 2</th></tr>"
            + "<tr><th>3</th><th>Name 3</th></tr>"
            + "<tr><th>4</th><th>Name 4</th></tr>"
            + "</table>"
            + "</font>");

    // add the attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath("/Users/alkandari/Desktop/SMART/Fahim/test_small.pdf");
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    email.attach(attachment);

    attachment = new EmailAttachment();
    attachment.setPath("/Users/alkandari/Desktop/SMART/Fahim/test.pdf");
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    email.attach(attachment);

    // send the email
    email.send();
    System.out.println("Mail sent!");
} catch (Exception e) {
    System.out.println("Exception :: " + e);
}

除了一切正常之外,HTML代码按原样显示。

在电子邮件中我得到的是

<font face='verdana' size='3'>Here is the picture you wanted <table><tr><th>id</th><th>Name</th></tr><tr><th>1</th><th>Name 1</th></tr><tr><th>2</th><th>Name 2</th></tr><tr><th>3</th><th>Name 3</th></tr><tr><th>4</th><th>Name 4</th></tr></table></font>

是否有任何参数,我将收到的电子邮件将具有正确的HTML格式的数据。

注:

实际上,我在使用Email email = new SimpleEmail();并在HTML部分运行正常的情况下进行了上述工作。但是,当我不得不切换到附件时,必须使用MultiPartEmail email = new MultiPartEmail();

java email html-email apache-commons email-attachments
3个回答
3
投票

我想在这里提供我的答案,以便将来我(和其他人)可以看到完整的代码。出于某种原因,可能仅仅是我,我发现这些答案不完整,或者对我没有用。像OP一样,我试图发送带有PDF附件的基于HTML的电子邮件。这是我最终使用commons-email 1.4的结果。任何意见将不胜感激。

进口:

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.EmailAttachment;

构建您的电子邮件对象(显然,这里的详细信息应该是您的)

MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.yourhosthere.com");
email.setSmtpPort(25);
// authentication not always needed depending on your environment
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setTo("[email protected]");
email.setFrom("[email protected]");

现在您的消息详细信息。请注意,我的HTML包含HTML和BODY标记。

email.setSubject("Your subject here");
email.addPart("<div>Your html here</div>", "text/html; charset=UTF-8");

现在是附件

EmailAttachment attachment = new EmailAttachment();
attachment.setPath(filepath);
attachment.setDisposition(EmailAttachment.ATTACHMENT);

email.attach(attachment);

现在发送电子邮件

email.send();

2
投票

我知道了。

仅将MultiPartEmail email = new MultiPartEmail();更改为MultiPartEmail email = new HtmlEmail();


1
投票

您不能使用email.setMsg()

但是将邮件正文设置为:

email.addPart( "<h1>MSG BODY</h1> <u>your</u> name", "text/html; charset=UTF-8" );

© www.soinside.com 2019 - 2024. All rights reserved.