简体   繁体   中英

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. To explain it quick and dirty, the mail is sent but there is no attachment at all when i look at it in 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

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 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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