[英]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.