现在我有电子邮件存储在数据库中,我需要能够检索它们并下载附件如果有的话。目前,我很难将将电子邮件存储的byte []转换为MailMessage或MimeMessage。我相信我已经将字节[]加入了Mimebody部分,但我不确定如何通过它解析并拉出附件。
final byte[] mailMessageString = resultSet.getBytes(mailMessageIndex);
File file = new File("C:\\Users\\khurt\\Downloads\\op.txt");
List<File> attachments = new ArrayList<File>();
@SuppressWarnings("deprecation")
String mimeType = file.toURL().openConnection().getContentType();
MimeBodyPart att = new MimeBodyPart();
ByteArrayDataSource efe = new ByteArrayDataSource(mailMessageString, mimeType);
DataHandler dh = new DataHandler(efe);
att.setDataHandler(dh);
att.setFileName(bds.getName());
Multipart multipart = (Multipart) att.getContent();
multipart.addBodyPart(att);
for (int i = 0; i < multipart.getCount(); i++)
{
BodyPart bodyPart = multipart.getBodyPart(i);
if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())
&& !StringUtils.isNotBlank(bodyPart.getFileName()))
{
InputStream is = bodyPart.getInputStream();
File f = new File("C:\\Users\\khurt\\Downloads\\"
+ bodyPart.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1)
{
fos.write(buf, 0, bytesRead);
}
fos.close();
attachments.add(f);
}
else
{
System.out.println("there is nata");
}
}
当它进入for循环时没有实际错误,但我还没有解析电子邮件以获取任何文件。是否可以解析MimeBodyPart?
对于参考,电子邮件将包含有关它的数据,然后电子邮件附件以空行开头,然后:(电子邮件inst java,Stack Overflow不允许我发布它而不格式化它。还有一些电子邮件没有包含附件信息的标题。
--_002_2733D716DEFD0D49BF462DE618263C07019302260BCVGEXCEMAIL01_
Content-Type: image/gif; name="image001.gif"
Content-Description: image001.gif
Content-Disposition: inline; filename="image001.gif"; size=1669;
creation-date="Tue, 14 Jun 2011 14:42:12 GMT";
modification-date="Tue, 14 Jun 2011 14:42:12 GMT"
Content-ID: <image001.gif@01CC2204.E828E6F0>
Content-Transfer-Encoding: base64
R0lGODlhiwA9ANUAAAAzZv/4+lvKwABVBzlstgY+q/wCBNebwwNN/l7H+ZsrHB0NuPkc/Z4kBm
jLEQFW+MqZ+yxQ8/b8wAANLb5NV/gu/y9eWwsl9/nzBZg8ZQU6+/z+/P0MFARPrv79BvcuCfobYg
JICZs+/39/Xf4NDZ47/M2ZGnvXCNqWGBoNaAgt6UlNBwcxFBcM7e5gAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAHAP8ALAAAAACLAD0AAAb/wIBw
SCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNtnoMcFBlTq9c4O189s2oaDoDgYKD
hIIPHR8XHnqMSgYMHx0PhYIhHZeYl5SDGiSNjRwMBQiElgUqIHhPcCoak4Ign2sGFYCCHacGHF0X
tg+esmS0tiEaKgbAYwWBDMFgJAyuxQwGi2kekyrOXBcfliqKjIAG21ceIAUFDLvBkuVV5+vvASEF
8/dVHAPJ+P1OBSz8CWxC4oG1gQiPnErIsEjBgw0ZAiQIp2K1iHpIDAg4hMOjWh1IbRKEoIMdjGhA
BDo0csChTJlErryAsoxKQ7hUULvYxEOv/0D8aoLxyM4cqRCphAr0oCLEoA6xlOLjU2ASAqkCOUzC
KjBECK6y3oAcRCoq2DbnNLjswELFsUcsNJxFWwEBiwtB5zK6wKLoPRMKMCgQkMBCAoYkIM5rcODA
AiELMgAQoZdRgxYAShQhQLlyngMAUhixECFABAcOFEhwIEBIAgwOBCsg0GCIhRYOhIhIDcEBAQUQ
WnhWYgIAgNJJjAvBAACF7slCTJgYEgHzYQEWAhTPHaBzCgDDi4gwPj05+AACAHAf37lICwWhiWwn
答案 0 :(得分:3)