如何使用JavaMail将文件附加到电子邮件 [英] How to attach a file to an email using JavaMail
本文介绍了如何使用JavaMail将文件附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用JavaMail发送PDF文件。 PDF目前是一个字节[]。如何将其导入DataSource?
I need to send a PDF file using JavaMail. The PDF is currently a byte[]. How do I get it into the DataSource?
byte[] pdffile = ....
messageBodyPart = new MimeBodyPart();
DataSource source = ???
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
推荐答案
使用 javax.mail.util.ByteArrayDataSource
:
DataSource source = new ByteArrayDataSource(pdffile, "application/pdf");
您可能知道,如果PDF在文件系统上,那么 FileDataSource
:
As you probably know, if the PDF is on the filesystem, it would be easier to the FileDataSource
:
DataSource source = new FileDataSource(pdfpath);
这篇关于如何使用JavaMail将文件附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文