【问题标题】:Apache commons-mail HtmlEmail with attachment: html is attached as noname.html in Gmail带有附件的 Apache commons-mail HtmlEmail:html 在 Gmail 中附加为 noname.html
【发布时间】:2014-07-31 09:37:51
【问题描述】:

我使用 apache commons-mail (v1.3.2) 发送带有 PDF 作为附件的订单确认电子邮件。 电子邮件在 Outlook(网络和桌面)中显示没有问题,但是当我发送到我的 gmail 帐户时,邮件内容为空,并且 Html 内容附加在单独的文件“noname.html”中。

我的代码:

       // Create mail instance using commons-mail and the hybris MailUtility class.
        HtmlEmail htmlEmail = (HtmlEmail) MailUtils.getPreConfiguredEmail(); // creates a mail instance with set mail
        htmlEmail.setCharset("UTF-8");
        htmlEmail.setHtmlMsg("this is <b>html text</b>);

        // Part two is attachment
            DataSource ds = new ByteArrayDataSource(mailAttachment.getData(), "application/pdf");
            htmlEmail.attach(ds, "attach.pdf", "generalconditions", EmailAttachment.ATTACHMENT);
        }

        //send mail
        htmlEmail.send();

起初,这个问题也出现在 Outlook 中,但我通过从 commons-mail v1.1 切换到 v1.3.2 解决了这个问题。 虽然仍然没有为 gmail 修复...

【问题讨论】:

  • 因为 getPreconfiguredEmail() 方法来自 Hybris MailUtils 类。我认为添加标签会很有用,以防万一这是一个已知的 hybris 问题。

标签: java gmail html-email hybris apache-commons-email


【解决方案1】:

你应该使用

EmailAttachment attachment = new EmailAttachment();
attachment.setPath(pdfFile.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);

然后你像这样将它附加到电子邮件中:

htmlEmail.attach(attachment);

【讨论】:

猜你喜欢
  • 2012-04-02
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
  • 2015-07-08
  • 2011-02-27
相关资源
最近更新 更多