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("[email protected]");
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();