我想在带有javax邮件的电子邮件中发送pdf文件 下面,baos是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();
当我尝试发送电子邮件时,我收到此异常:
javax.mail.MessagingException:发送消息时发生IOException;
没有对象DCH
嵌套异常是:javax.activation.UnsupportedDataTypeException: MIME类型application / pdf
我不知道这里有什么问题 如果有人......
提前致谢。
答案 0 :(得分:2)
试试这个。
MimeBodyPart attachment = new MimeBodyPart();
DataSource dataSrc = new ByteArrayDataSource(baos.toByteArray(), "application/pdf");
attachment.setDataHandler(new DataHandler(dataSrc));
attachment.setFileName("myPdfDocument.pdf");
multipart.addBodyPart(attachment);
答案 1 :(得分:0)
每次我这样做,我都直接使用字节数组,而无需通过文件:
byte [] data = baos.toByteArray();
MimeBodyPart attachBodyPart = new MimeBodyPart();
attachBodyPart.setFileName(fileName);
attachBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
attachBodyPart.setContent(data, "application/pdf");
multipart.addBodyPart(attachBodyPart);