อ่านไฟล์แนบอีเมลที่ฝังในข้อความด้วย Aspose.Email

Aspose.Email - อ่านไฟล์แนบอีเมลที่ฝังจากข้อความ

บางครั้ง เราได้รับอีเมลที่มีอีเมลอื่นฝังอยู่ในรูปแบบไฟล์แนบ อีเมลที่ฝังเหล่านี้เป็นข้อความที่สมบูรณ์พร้อมรายชื่อผู้รับ หัวเรื่อง เนื้อหา และแม้กระทั่งไฟล์แนบแต่ละข้อความยังสามารถมีข้อความฝังเพิ่มเติมได้ ด้วย Aspose.Email Java นักพัฒนาสามารถเข้าถึงข้อความที่ฝังแต่ละข้อความได้เป็นอิสระ ตัวอย่างนี้แสดงการใช้ฟังก์ชันเชิงซ้อน

Java

 MailMessage message = MailMessage.load(dataDir + \"embedded.msg\", MessageFormat.getMsg());

for (int i = 0; i < message.getAttachments().size(); i++)

{

    Attachment att = (Attachment) message.getAttachments().get_Item(i);

    System.out.println("Attachment Name: " + att.getName());

    // Get the name of attachment. If msg subject contains characters like :, /, \ etc., replace with space

    // because windows cannot save files with these characters

    // also save first 50 characters as file name to avoid long file names

    String attFileName = att.getName().replace(".eml", "").replace(":", " ").replace("\\", " ").replace("/", " ").replace("?", "");

    if (attFileName.length() > 50)

    {

        attFileName = attFileName.substring(0, 50);

    }

    String attExt = (att.getName().substring(att.getName().lastIndexOf("."), att.getName().lastIndexOf(".") + 4));

    // Save the attachment to disk

    att.save(dataPath + attFileName + attExt);

    // Check if it is an orphaned text attachment file (ATT00001.txt....) and of type eml

    if ((attExt.equals(".eml")) || (att.getContentType().getMediaType().equals("text/plain") && att.getName().contains(".txt") == true && att.getName().contains("ATT") == true))

    {

        // Try to load this text file in MailMessage

        MailMessage attMsg = MailMessage.load(dataPath + attFileName + attExt, MessageFormat.getEml());

        // Call the function recursively to parse this message and attachments

        ParseMessage(attMsg);

    }

}

ดาวน์โหลดโค้ดที่ทำงาน

ดาวน์โหลดโค้ดตัวอย่าง