Jave:带有pdf附件的电子邮件:MIME类型为application/pdf的无对象DCH [英] Jave : email with pdf attached file : no object DCH for MIME type application/pdf
本文介绍了Jave:带有pdf附件的电子邮件:MIME类型为application/pdf的无对象DCH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Javax邮件通过电子邮件发送pdf文件.
在下面,baos是一个ByteArrayOutputStream.
I want to send a pdf file in an email with javax mail.
Below, baos is a ByteArrayOutputStream.
byte [] data= baos.toByteArray();
OutputStream output = new FileOutputStream(fileName);
output.write(data);
output.close();
DataSource source = new FileDataSource(fileName);
attachBodyPart.setDataHandler(new DataHandler(source, "application/pdf"));
attachBodyPart.setFileName(fileName);
multipart.addBodyPart(attachBodyPart);
message.setContent(multipart, "text/html");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
当我尝试发送电子邮件时出现此异常:
I am getting this exception when I try to send the email :
javax.mail.MessagingException:发送消息时IOException;
嵌套的异常是:javax.activation.UnsupportedDataTypeException:MIME类型为application/pdf的无对象DCH
javax.mail.MessagingException: IOException while sending message;
nested exception is: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/pdf
我不知道这是怎么回事.
如果有人这样做...
I don't know what's wrong here.
If someone does...
谢谢.
推荐答案
尝试一下.
MimeBodyPart attachment = new MimeBodyPart();
DataSource dataSrc = new ByteArrayDataSource(baos.toByteArray(), "application/pdf");
attachment.setDataHandler(new DataHandler(dataSrc));
attachment.setFileName("myPdfDocument.pdf");
multipart.addBodyPart(attachment);
这篇关于Jave:带有pdf附件的电子邮件:MIME类型为application/pdf的无对象DCH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文