[英]ParseException when sending an email with zip file attachment?
I am getting an Exception when sending an email with a zipped file attachment, any suggestions? 发送带有压缩文件附件的电子邮件时出现异常,有什么建议吗?
Caused by: javax.mail.internet.ParseException: Expected '/', got null at javax.mail.internet.ContentType.(ContentType.java:102) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1322) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021) at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:13
引起原因:javax.mail.internet.ParseException:预期为'/',javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1322)处的javax.mail.internet.ContentType。(ContentType.java:102)为null )的javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)的javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)的javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java: 13
private MimeBodyPart makeZipAttachment(AttachmentInfo attachmentInfo) throws IOException, MessagingException {
ByteArrayOutputStream bos = null;
ZipOutputStream zip = null;
try
{
bos = new ByteArrayOutputStream();
zip = new ZipOutputStream(bos);
zip.putNextEntry(new ZipEntry(attachmentInfo.getName()));
InputStream inputStream = attachmentInfo.getAttachment().getInputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
zip.write(buffer, 0, len);
}
zip.closeEntry();
}
finally
{
if (bos != null)
bos.close();
if (zip != null)
zip.close();
}
DataSource dataSource = new ByteArrayDataSource(bos.toByteArray(), "application/zip");
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
mimeBodyPart.setFileName(attachmentInfo.getName() + ".zip");
mimeBodyPart.setHeader(CONTENT_TYPE, "application/zip");
return mimeBodyPart;
}
在我自己运行程序之前不能说太多,但是尝试设置内容以及像mimeBodyPart.setContent这样的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.