繁体   English   中英

使用Apache Commons电子邮件在我的硬盘上发送带有附件的邮件

[英]Send a mail with an attachment on my hard drive with Apache commons email

我在使用Apache Commons电子邮件在邮件中发送附件时遇到问题。 为了解释它的快速和肮脏,我发送了邮件,但是当我在Outlook中查看邮件时根本没有附件。

我使用Apache Commons Email v1.4和JAVA8。我想在硬盘驱动器上的此位置C:\\ myfolder \\ myfile.log上添加一个日志文件。

到目前为止,这是我尝试添加的附件

Path logRejetPath = Paths.get("C:\\myfolder\\myfile.log");
Boolean pathExists = Files.exists(logRejetPath, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});

if (pathExists) {
   File rejLogFile = new File(logRejetPath.toString());
   email.attach(new FileDataSource(rejLogFile), "test", "test");                
}
email.send();

要么

Path logRejetPath = Paths.get("C:\\myfolder\\myfile.log");
Boolean pathExists = Files.exists(logRejetPath, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});

if (pathExists) {
   File rejLogFile = new File(logRejetPath.toString());
   email.attach(rejLogFile);                
}
email.send();

要么

Path logRejetPath = Paths.get("C:\\myfolder\\myfile.log");
Boolean pathExists = Files.exists(logRejetPath, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});

if (pathExists) {
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(logRejetPath.toString());
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("test");
    attachment.setName("test");
    email.attach(attachment);              
}
email.send();

我精确的电子邮件是这样创建的MultiPartEmail对象:

MultiPartEmail email = new MultiPartEmail();

    try {
        email.setHostName(config.getSmtpHost()); 
        email.setSmtpPort(Integer.valueOf(config.getSmtpPort()));
        if (!config.getSmtpUser().isEmpty()) {
            email.setAuthenticator(
                    new DefaultAuthenticator(config.getSmtpUser(), config.getSmtpPwd()));
            email.setSSLOnConnect(true);
        } else {
            email.setSSLOnConnect(false);
        }
        email.setCharset("utf-8");
        email.setFrom("me@me.fr");
        email.setSubject("subjectforemail");
        email.setContent(this.getMessage(), "text/html");

        final String[] destinataires = config.getMailDestinataires().split(";");
        for (final String dest : destinataires) {
            email.addTo(dest);
        }

每次使用这些不同的方法添加附件时,我都会收到带有邮件的电子邮件,但没有附件。 每次变量pathExists为TRUE,每次我都没有错误。

感谢您日后的回答和帮助。

编辑:通过更改此找到的解决方案:

MultiPartEmail email = new MultiPartEmail();

这样 :

HtmlEmail email = new HtmlEmail();

通过更改此方法找到的解决方案:

MultiPartEmail email = new MultiPartEmail();

这样 :

HtmlEmail email = new HtmlEmail();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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