استخراج مرفقات الرسالة باستخدام Aspose.Email و Apache POI HSMF
Contents
[
Hide
]
Aspose.Email - استخراج مرفقات الرسالة
لحفظ المرفقات من الرسائل الموجودة:
- إنشاء نسخة من فئة MailMessage.
- حمّل رسالة البريد الإلكتروني الحالية باستخدام طريقة load() في فئة MailMessage، مع تحديد MessageFormat الصحيح.
- أنشئ مثيلاً من فئة AttachmentCollection واملأه بالمرفقات من كائن MailMessage باستخدام طريقة getAttachments().
- تجول في مجموعة AttachmentCollection.
- أنشئ مثيلاً من فئة Attachment واملأه بالقيمة المفهرسة من AttachmentCollection باستخدام طريقة get().
- احفظ المرفق على القرص باستخدام طريقة save() في فئة Attachment.
Java
MailMessage message = MailMessage.load(dataDir + \"message.msg\");
System.out.println(\"Extracting attachments....\");
for (int i = 0; i < message.getAttachments().size(); i++)
{
Attachment att = (Attachment) message.getAttachments().get_Item(i);
System.out.println("Attachment Name: " + att.getName());
String attFileName = att.getName().replace(".eml", "").replace(":", " ").replace("\\", " ").replace("/", " ").replace("?", "");
// Save the attachment to disk
att.save(dataDir + attFileName);
}Apache POI HSMF - استخراج مرفقات الرسالة
يمكن استخدام فئة AttachmentChunks للوصول إلى مرفقات MAPIMessage.
Java
MAPIMessage msg = new MAPIMessage(dataDir + \"message.msg\");
AttachmentChunks[] attachments = msg.getAttachmentFiles();
if (attachments.length > 0)
{
File d = new File(dataDir + "attachments");
if (d.exists() || d.mkdir())
{
for (AttachmentChunks attachment : attachments)
{
String fileName = attachment.attachFileName.toString();
if (attachment.attachLongFileName != null)
{
fileName = attachment.attachLongFileName.toString();
}
File f = new File(d, fileName);
OutputStream fileOut = null;
try
{
fileOut = new FileOutputStream(f);
fileOut.write(attachment.attachData.getValue());
}
finally
{
if (fileOut != null)
{
fileOut.close();
}
}
}
}
}تحميل الكود القابل للتنفيذ
تحميل عينة الكود
لمزيد من التفاصيل، زر إدارة المرفقات في رسالة البريد الإلكتروني.