简体   繁体   English

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

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

I have a problem to send an attachment in my mail with Apache commons email. 我在使用Apache Commons电子邮件在邮件中发送附件时遇到问题。 To explain it quick and dirty, the mail is sent but there is no attachment at all when i look at it in Outlook. 为了解释它的快速和肮脏,我发送了邮件,但是当我在Outlook中查看邮件时根本没有附件。

I use Apache commons email v1.4 and JAVA 8. I want to add a log file which is on my hard drive at this location C:\\myfolder\\myfile.log 我使用Apache Commons Email v1.4和JAVA8。我想在硬盘驱动器上的此位置C:\\ myfolder \\ myfile.log上添加一个日志文件。

This is what i have tried so far to add the attachment 到目前为止,这是我尝试添加的附件

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

Or 要么

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

Or 要么

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

I precise email is a MultiPartEmail object created like this: 我精确的电子邮件是这样创建的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);
        }

Every time with these different methods to add an attachment, i receive my email with the message but without the attachment. 每次使用这些不同的方法添加附件时,我都会收到带有邮件的电子邮件,但没有附件。 Every time, variable pathExists is TRUE and every times i have no error. 每次变量pathExists为TRUE,每次我都没有错误。

Thanks for your future answers and help. 感谢您日后的回答和帮助。

EDIT : Solution found by changing this : 编辑:通过更改此找到的解决方案:

MultiPartEmail email = new MultiPartEmail();

by this : 这样 :

HtmlEmail email = new HtmlEmail();

Solution found by changing this : 通过更改此方法找到的解决方案:

MultiPartEmail email = new MultiPartEmail();

by this : 这样 :

HtmlEmail email = new HtmlEmail();

暂无
暂无

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

相关问题 Apache Commons CSV 解析器 - Apache commons CSV parser Apache Commons CSV 解析器:无法读取值 - Apache Commons CSV parser: Not able to read the values Apache Commons CollectionsUtils.select的Java 8 Lambda替换 - Java 8 Lambda Replacement for Apache Commons CollectionsUtils.select 在Apache commons-math3中为PolynomialCurveFitter计算R平方 - Calculate R-Square for PolynomialCurveFitter in Apache commons-math3 如何使用 Eclipse 安装 org.apache.commons.cli - How to install org.apache.commons.cli using Eclipse 如何使用Apache Commons CSV Java忽略CSV文件最后一行中的记录? - How to ignore a record in the last line of the CSV file using Apache Commons CSV java? 为什么Apache Commons VFS考虑使用Http代理和Socks5代理,而只忽略Socks4代理? - Why does Apache Commons VFS consider Http Proxy and Socks5 Proxy but simply ignore Socks4 Proxy? Hibernate和Spring Boot MalformedParameterizedTypeException:初始化bean时为null(org.apache.commons.dbcp2.BasicDataSource) - Hibernate and Spring Boot MalformedParameterizedTypeException: null while initializing bean (org.apache.commons.dbcp2.BasicDataSource) ClassNotFoundException: org.apache.commons.dbcp.BasicDataSourceFactory- 关于将 tomcat 从版本 7 升级到 9 - ClassNotFoundException: org.apache.commons.dbcp.BasicDataSourceFactory- on upgrading tomcat from version 7 to 9 Maven提供了org.apache.maven.plugins:maven-source-plugin:3.0.1:jar:org / apache / commons / io / output / DeferredFileOutputStream - Maven gives org.apache.maven.plugins:maven-source-plugin:3.0.1:jar:org/apache/commons/io/output/DeferredFileOutputStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM