![](/img/trans.png)
[英]Send email with javax.mail using an existing InputStream as attachment content
[英]How parse email content with javax.mail?
我正在尝试使用javax.mail解析电子邮件。 我想同时获取文本内容和所有附件(最好还有INLINE图片/附件)。
我有以下代码,但它似乎与带有多个嵌套多部分的更复杂的邮件中断了。
我已经阅读了常见问题解答,并整日在Google上搜索,但没有找到解决方案。
请帮忙。
public static String fetchEmailcontent(Part message, String messageid) throws IOException, MessagingException {
StringWriter sw = new StringWriter(1024);
if (message != null && message.getContent() != null) {
if (message.getContent() instanceof Multipart) {
Multipart parts = (Multipart) message.getContent();
BodyPart p;
boolean alternative = parts.getContentType().trim().toLowerCase().startsWith("multipart/alternative") ? true : false;
InputStreamReader isr;
int retrieved;
char[] buffer = new char[512];
for (int i = 0; i < parts.getCount(); i++) {
p = parts.getBodyPart(i);
if (p.getContentType().toLowerCase().startsWith("multipart")) {
sw.write(fetchEmailcontent(p, messageid));
break;
} else if ((Part.INLINE.equalsIgnoreCase(p.getDisposition()) || p.getDisposition() == null) && p.getContentType().toLowerCase().startsWith("text") && p.getFileName() == null) {
if (InputStream.class.isInstance(p.getContent())) {
InputStream ip = p.getInputStream();
StringWriter subwriter = new StringWriter(ip.available());
isr = new InputStreamReader(ip);
while (isr.ready()) {
retrieved = isr.read(buffer, 0, 512);
subwriter.write(buffer, 0, retrieved);
}
sw.write(subwriter.toString());
} else {
Object content = p.getContent();
if (java.io.ByteArrayInputStream.class.isInstance(content)) {
int bcount = ((java.io.ByteArrayInputStream) content).available();
byte[] c = new byte[bcount];
((java.io.ByteArrayInputStream) content).read(c, 0, bcount);
sw.write(new String(c));
} else {
sw.write(content.toString());
}
}
if (alternative && !"".equals(sw.toString().trim())) {
break;
}
sw.write("\r\n");
} else if (p.getDisposition() != null && (p.getDisposition().equalsIgnoreCase(Part.ATTACHMENT) || p.getDisposition().equalsIgnoreCase(Part.INLINE))) {
saveFile(MimeUtility.decodeText(p.getFileName()), p.getInputStream(), messageid);
}
}
} else if (message.getContentType().toLowerCase().startsWith("text")) {
sw.write(message.getContent().toString());
}
}
return sw.toString();
}
这是一个无法从中获取附件的邮件示例:(我删除了附件的标头和BASE64代码以节省空间。否则,它们就很好了)
Content-Type: multipart/mixed; boundary=f46d04016b4779522904c58fb5b4
--f46d04016b4779522904c58fb5b4
Content-Type: multipart/alternative; boundary=f46d04016b4779522104c58fb5b2
--f46d04016b4779522104c58fb5b2
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
test
sdljpjdpjsd
=E5=E4=F6
--f46d04016b4779522104c58fb5b2
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div>test </div><div>=A0</div><div>=A0</div><div>sdljpjdpjsd</div><div>=A0<=
/div><div>=A0</div><div>=E5=E4=F6</div>
--f46d04016b4779522104c58fb5b2--
--f46d04016b4779522904c58fb5b4
Content-Type: image/jpeg; name="blah.jpg"
Content-Disposition: attachment; filename="blah.jpg"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_h50rhk180
BUNCH OF BASE64
--f46d04016b4779522904c58fb5b4
Content-Type: application/pdf; name="blah2.pdf"
Content-Disposition: attachment; filename="blah2.pdf"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_h50ria042
BUNCH OF BASE64
--f46d04016b4779522904c58fb5b4--
预期的输出是邮件正文中的文本以及所有附件已保存到磁盘。 函数saveFile()可以做到这一点,但它的基本意义使我决定不包括它。 我肯定不是罪魁祸首。
提前致谢。
在您的代码中...
if (InputStream.class.isInstance(p.getContent()))
可以为false,则InputStream ip = p.getInputStream();
仍然可以成功!
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.