【发布时间】:2016-01-22 14:10:10
【问题描述】:
我想知道我在这里做错了什么(我错过了什么)。我的电子邮件确实发出了,但没有附件。我发送的文件简称为“日志”。正如您将看到的,我在这里尝试了多种方法,我也一一尝试了它们,但它们都不起作用:
MimeMessage message = new MimeMessage(session);
MimeBodyPart emailAttachment = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
int len = build.getLogFile().getPath().length();
//I have verified that "file" provides the right path
String file = build.getLogFile().getPath().substring(0, (len-3));
String fileName = "log";
DataSource source = new FileDataSource(file);
emailAttachment.setDataHandler(new DataHandler(source));
//I know this .attachFile is not needed but I added it when nothing was working
emailAttachment.attachFile(build.getLogFile());
emailAttachment.setFileName(fileName);
multipart.addBodyPart(emailAttachment);
message.setContent(multipart);
message.setFrom(adminAddress);
message.setText(builder.toString());
message.setSentDate(new Date());
mailSender.send(message);
谢谢
【问题讨论】:
-
您是否确定您所引用的文件存在并且也有内容?除了这种可能性,您的代码看起来还不错。
-
我认为您应该尝试调试并逐行逐步执行您的发送方法。
-
我确实通过尝试捕获文件是否为空来验证文件是否存在。它不为空。我可以手动转到该目录并找到名为 log 的文件。它确实有上下文,但没有文件扩展名。它没有文件扩展名这一事实会有所不同吗?
标签: java file attachment