使用Java Mail API保存电子邮件(包括图像和HTML数据)的最佳方法?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Java Mail API保存电子邮件(包括图像和HTML数据)的最佳方法?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2116字,纯文字阅读大概需要4分钟。
内容图文
![使用Java Mail API保存电子邮件(包括图像和HTML数据)的最佳方法?](/upload/InfoBanner/zyjiaocheng/792/48d29bd44957469fa3c2032006e756b0.jpg)
我正在寻找保存包含内嵌图像和HTML内容的电子邮件正文的最佳方法.我想保留邮件中包含的所有内容.
My ultimate Goal is to save the complete email body into a PDF
如果有直接的方法将电子邮件正文写入PDF?
如果不是什么是保存电子邮件的最佳格式?
我可以使用其他一些可用的API将HTML,DOC等转换为PDF.
private void downloadAttachment(Part part, String folderPath) throws Exception {
String disPosition = part.getDisposition();
String fileName = part.getFileName();
String decodedText = null;
logger.info("Disposition type :: " + disPosition);
logger.info("Attached File Name :: " + fileName);
if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) {
logger.info("DisPosition is ATTACHMENT type.");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName != null && disPosition == null) {
logger.info("DisPosition is Null type but file name is valid. Possibly inline attchment");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName == null && disPosition == null) {
logger.info("DisPosition is Null type but file name is null. It is email body.");
File file = new File(folderPath + File.separator + "mail.html");
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
}
}
protected int saveEmailAttachment(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = null;
InputStream is = null;
int ret = 0, count = 0;
try {
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
part.writeTo(new FileOutputStream(saveFile));
} finally {
try {
if (bos != null) {
bos.close();
}
if (is != null) {
is.close();
}
} catch (IOException ioe) {
logger.error("Error while closing the stream.", ioe);
}
}
return count;
}
请建议.谢谢!
解决方法:
将其保存为自然状态,作为MimeMessage.
JavaMail MimeMessages可以流式传输到文本,因为这是他们到达邮件的方式.例如,MimeMessage.writeTo将消息保存为文本.同样,MimeMessage.parse将其重新读入.在MimeMessage中,您可以非常轻松地获取文本,附件等.
您也可以将其作为序列化Java对象进行流式传输,但是,坦率地说,我不会.文本表示更有用.
内容总结
以上是互联网集市为您收集整理的使用Java Mail API保存电子邮件(包括图像和HTML数据)的最佳方法?全部内容,希望文章能够帮你解决使用Java Mail API保存电子邮件(包括图像和HTML数据)的最佳方法?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。