1

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

String myEmailId = "xx@yahoo.co.in";
String myPassword = "@xx";
String senderId = "yy@gmail.com";
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();.

4

3 回答 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("to@yourhosthere.com");
email.setFrom("from@yourhosthere.com");

现在您的消息详细信息。请注意,我的 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();
于 2015-12-03T02:38:36.790 回答
2

我得到了答案。

MultiPartEmail email = new MultiPartEmail();改成MultiPartEmail email = new HtmlEmail();

于 2012-11-02T11:29:55.713 回答
1

你不能使用email.setMsg()

但将消息正文设置为:

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

于 2015-08-20T16:29:33.977 回答