תכנות עם Thunderbird

קרא הודעות מ‑MBOX

Mozilla Thunderbird הוא לקוח דוא"ל קוד פתוח, חוצה פלטפורמות, שפותח על ידי Mozilla Foundation. הוא מאחסן הודעות במבנה קבצים משלו, מנהל אינדקסי הודעות ותתי‑תיקיות באמצעות פורמטים קבצים קנייניים. Aspose.Email יכולה לעבוד עם מבני האחסון של Thunderbird. ה‑ MboxrdStorageReader מחלקה מאפשרת למפתחים לקרוא הודעות מקובץ האחסון של Mozilla Thunderbird. מאמר זה מציג כיצד לקרוא את ההודעות מאחסון האימייל של Thunderbird:

  1. פתח את קובץ האחסון של Thunderbird ב-FileStream.
  2. צור מופע של MboxrdStorageReader המחלקה והעבר את הזרם למעלה לבנאי.
  3. קרא ReadNextMessage() כדי לקבל את ההודעה הראשונה.
  4. השתמש באותו ReadNextMessage() במחזור while לקריאת כל ההודעות.
  5. סגור את כל הזרמים.

קטע הקוד הבא מציג כיצד לקרוא את כל ההודעות מאחסון הדואר של Thunderbird.

//Getting Marker information while reading messages from Mbox storage file
try (FileInputStream stream = new FileInputStream(dataDir + "Outlook.pst")) {
    MboxLoadOptions lo = new MboxLoadOptions();
    lo.setLeaveOpen(false);
    try (MboxrdStorageReader reader = new MboxrdStorageReader(stream, lo)) {
        MailMessage msg;
        String[] fromMarker = {null};
        while ((msg = reader.readNextMessage(/* out */fromMarker)) != null) {
            System.out.println(fromMarker[0]);
        }
    }
}

קבע אפשרויות טעינה בעת קריאת הודעות מ‑MBOX

API של Aspose.Email מאפשר את המניפולציות הבאות על הודעות בעת קריאתן מתוך קובץ MBOX:

המר הודעות מ‑MBOX ל‑PST תוך שמירת קבצי צירוף TNEF

EmlLoadOptions emlLoadOptions = new EmlLoadOptions();
emlLoadOptions.setPreserveTnefAttachments(true);
MailStorageConverter.setMboxMessageOptions(emlLoadOptions);
// Convert messages from mbox to pst preserving tnef attachments.
PersonalStorage storage = MailStorageConverter.mboxToPst("Input.mbox", "Output.pst");

MailStorageConverter.MboxMessageOptions() מאפיין - מקבל או מגדיר אפשרויות טעינת אימייל בעת ניתוח אחסון Mbox.

קרא הודעות תוך שמירת צירופי TNEF

MboxrdStorageReader reader = new MboxrdStorageReader("Input.mbox", new MboxLoadOptions());
// Read messages preserving tnef attachments.
EmlLoadOptions emlLoadOptions = new EmlLoadOptions();
emlLoadOptions.setPreserveTnefAttachments(true);
MailMessage eml = reader.readNextMessage(emlLoadOptions);

MboxrdStorageReader.readNextMessage(EmlLoadOptions options) מתודה - פרמטר EmlLoadOptions מציין אפשרויות בעת קריאת הודעה מאחסון Mbox.

ספירת הודעות תוך שמירת קבצי צירוף TNEF

EmlLoadOptions emlLoadOptions = new EmlLoadOptions();
emlLoadOptions.setPreserveTnefAttachments(true);
// Enumerate messages preserving tnef attachments.
for (MailMessage message : reader.enumerateMessages(emlLoadOptions)) {
    // do something
}

MboxrdStorageReader.enumerateMessages(EmlLoadOptions options) מתודה - מציינת EmlLoadOptions בעת קריאת הודעה מאחסון Mbox.

חלץ הודעות מ‑MBOX לפי מזהים

לעתים נדרש לחלץ הודעות נבחרות לפי מזהים. לדוגמה, האפליקציה שלך מאחסנת מזהים בבסיס נתונים וחולצת הודעה לפי דרישה. זהו הדרך היעילה למניעת חיפוש בכל האחסון בכל פעם כדי למצוא הודעה ספציפית לחילוץ. כדי ליישם תכונה זו לקבצי MBOX, Aspose.Email מספקת את השיטות והמחלקות הבאות:

דוגמת הקוד שלמטה מדגימה כיצד לחלץ הודעות מ‑MBOX לפי מזהים:

MboxStorageReader reader = MboxStorageReader.createReader("my.mbox", new MboxLoadOptions());

for (MboxMessageInfo msgInfo : reader.enumerateMessageInfo()) {
    MailMessage eml = reader.extractMessage(msgInfo.getEntryId(), new EmlLoadOptions());
}

הערה: מזהה ההודעה הוא ייחודי בתוך קובץ האחסון. המזהים נוצרים על‑ידי Aspose.Email ולא ניתן להשתמש בהם בספריות או אפליקציות צד שלישי אחרות לעיבוד MBOX.

גישה למטא‑נתוני הודעה

Aspose.Email מספקת את MboxMessageInfo מחלקה שנועדה לגשת למטא‑נתונים בסיסיים של הודעה מבלי לקרוא את תוכן ה‑MailMessage המלא. היא משמשת כאובייקט סיכום ומוחזרת בעת קריאה ל MboxStorageReader.enumerateMessageInfo().

מתודות מפתח הנחשפות על‑ידי MboxMessageInfo כדי לאסוף מידע על הודעה כולל:

  • getDate() - מקבל את תאריך ההודעה
  • getFrom() - מקבל את כתובת השולח
  • getSubject() - מקבל את נושא ההודעה
  • getTo() - מקבל את אוסף הכתובות שמכיל את נמען ההודעה
  • getCC() - מקבל את אוסף הכתובות שמכיל נמעני CC
  • getBcc() - מקבל את אוסף הכתובות שמכיל נמעני BCC של ההודעה
  • getEntryId() - מקבל את המזהה שניתן להעביר ל‑ extractMessage
MboxStorageReader reader = MboxStorageReader.createReader("input.mbox", new MboxLoadOptions());

for (MboxMessageInfo mboxMessageInfo : reader.enumerateMessageInfo()) {
    System.out.println("Subject: " + mboxMessageInfo.getSubject());
    System.out.println("Date: " + mboxMessageInfo.getDate());
    System.out.println("From: " + mboxMessageInfo.getFrom());
    System.out.println("To: " + mboxMessageInfo.getTo());
    System.out.println("CC: " + mboxMessageInfo.getCC());
    System.out.println("Bcc: " + mboxMessageInfo.getBcc());
}

סינון וחיפוש אימיילים בקובצי MBOX

Aspose.Email for Java מספקת אפשרות לסנן או לחפש הודעות בתוך קובצי MBOX באמצעות שאילתה. זה מאפשר לאחזר רק את ההודעות התואמות לקריטריונים ספציפיים. כך ניתן לשפר את ביצועי האפליקציה ואת השימושיות בעת עבודה עם קבצי MBOX גדולים.

דוגמת הקוד שלמטה מדגימה כיצד ליישם תכונה זו באמצעות מימוש השיטות הבאות:

  • enumerateMessages(MailQuery query) - מחזיר אוסף ניתנת לחזרה של מופעי MailMessage התאימים לשאילתה המוגדרת.

  • enumerateMessageInfo(MailQuery query) - מחזיר אוסף ניתנת לחזרה של מופעי MboxMessageInfo התאימים לשאילתה המוגדרת.

MboxStorageReader reader = MboxStorageReader.createReader("input.mbox", new MboxLoadOptions());
MailQueryBuilder mqb = new MailQueryBuilder();
mqb.getSubject().contains("Project Update");
mqb.getSentDate().before(new Date());

for (MailMessage message : reader.enumerateMessages(mqb.getQuery())) {
    System.out.println("Subject: " + message.getSubject());
}

אחזור הודעות בעמודים מקובצי MBOX

Aspose.Email for Java תומכת באחזור הודעות בדף ממקובץ MBOX. תכונה זו מאפשרת עיבוד יעיל של קבצי MBOX גדולים על ידי אחזור הודעות במנות קטנות יותר, מצמצמת שימוש בזיכרון ומשפרת ביצועים.

דוגמת הקוד שלמטה מדגימה כיצד ליישם תכונה זו באמצעות מימוש השיטות הבאות:

  • enumerateMessages(int startIndex, int count) - מאחזר מספר מוגדר של מופעי MailMessage החל מאינדקס מוגדר.

  • enumerateMessageInfo(int startIndex, int count) - מאחזר מספר מוגדר של מופעי MboxMessageInfo החל מאינדקס מוגדר.

MboxStorageReader reader = MboxStorageReader.createReader("input.mbox", new MboxLoadOptions());
int startIndex = 0;
int count = 10; // Retrieve messages in batches of 10

for (MailMessage message : reader.enumerateMessages(startIndex, count)) {
    System.out.println("Subject: " + message.getSubject());
}

כתיבת הודעות

ה MboxrdStorageWriter מחלקה מספקת אפשרות לכתוב הודעות חדשות לקובץ האחסון של הדואר של Thunderbird. כדי לכתוב הודעות:

  1. פתח את קובץ האחסון של Thunderbird ב-FileStream.
  2. צור מופע של MboxrdStorageWriter המחלקה והעבר את הזרם למעלה לבנאי.
  3. הכן הודעה חדשה באמצעות MailMessage מחלקה.
  4. הפעל את WriteMessage() מתודה והעבר את המעלה MailMessage מופע להוספת ההודעה לאחסון Thunderbird.
  5. סגור את כל הזרמים.

קטודת הקוד שלהלן מראה כיצד לכתוב הודעות לאחסון הדואר של Thunderbird.

//Getting marker information while writing messages to Mbox storage file
try (FileOutputStream writeStream = new FileOutputStream(dataDir + "inbox")) {
    try (MboxrdStorageWriter writer = new MboxrdStorageWriter(writeStream, new MboxSaveOptions())) {
        MailMessage msg = MailMessage.load(dataDir + "Message.msg");
        String[] fromMarker = {null};
        writer.writeMessage(msg, fromMarker);
        System.out.println(fromMarker[0]);
    }
}

קבלת מספר ההודעות הכולל מקובץ MBox

ה MboxrdStorageReader מחלקה מספקת את האפשרות לקרוא את מספר הפריטים הזמינים בקובץ MBox. ניתן להשתמש בכך לפיתוח אפליקציות המציגות את התקדמות הפעילות בעת עיבוד קובץ זה.

MboxLoadOptions lo = new MboxLoadOptions();
try (MboxrdStorageReader reader = new MboxrdStorageReader("inbox.dat", lo)) {
    System.out.println("Total number of messages in Mbox file: " + reader.getTotalItemsCount());
}

קבל את גודל ההודעה הנוכחי

FileInputStream stream = new FileInputStream(dataDir + "ExampleMbox.mbox");
MboxLoadOptions lo = new MboxLoadOptions();
try (MboxrdStorageReader reader = new MboxrdStorageReader(stream, lo)) {
    MailMessage msg = null;
    while ((msg = reader.readNextMessage()) != null) {
        //returns the number of bytes read
        long currentDataSize = reader.getCurrentDataSize();
        System.out.println("Bytes read: " + currentDataSize);
    }
}

הגדר קידוד טקסט מועדף בעת טעינת קבצי Mbox

ה setPreferredTextEncoding(Charset value) שיטה של MboxLoadOptions מחלקה מקבלת או מגדירה קידוד מועדף עבור הודעות. צור קורא לקובץ Mbox עם אפשרויות טעינה מוגדרות וההודעות שלך עם תוכן מקודד ייקראו ויעובדו כראוי. דוגמת הקוד שלהלן מציגה כיצד ליישם תכונה זו בפרויקט:

MboxLoadOptions lo = new MboxLoadOptions();
lo.setPreferredTextEncoding(Charset.forName("utf-8"));
MboxrdStorageReader reader = new MboxrdStorageReader("sample.mbox", lo);
MailMessage message = reader.readNextMessage();

פצל אחסון Mbox לחלקים קטנים יותר

Aspose.Email מספקת את הרכיבים הבאים שמעוצבים לתת שליטה רבה יותר על עיבוד אחסון Mbox, מאפשרים לחלק קבצים גדולים לחלקים שניתן לנהל וליישם פעולות מותאמות במהלך התהליך:

MboxStorageReader.SplitInto(long chunkSize, String outputPath, String partFileNamePrefix) גִירסה של השיטה הקודמת, גרסה זו מאפשרת גם לציין קידומת מותאמת אישית לשמות קבצי ה‑Mbox המחולקים.

MboxStorageReader.setEmlCopyingEventHandler אירוע אירוע זה מתרחש לפני שהודעה מועתקת לקובץ Mbox חדש. ניתן להתאים פעולות לפני עיבוד הודעות.

MboxStorageReader.setEmlCopiedEventHandler אירוע אירוע זה מתרחש לאחר שהודעה נעתקה לקובץ Mbox חדש. ניתן לבצע פעולות עיבוד שלאחר מכן על הודעות.

MboxStorageReader.setMboxFileCreatedEventHandler אירוע אירוע זה מתרחש כאשר קובץ Mbox חדש נוצר. ניתן לטפל באירוע זה כדי להגיב ליצירת הקובץ.

MboxStorageReader.setMboxFileFilledEventHandler Event אירוע זה מתרחש כאשר קובץ Mbox חדש מתמלא במיילים. ניתן להגיב למילוי הקובץ במיילים.

NewStorageEventHandler(Object sender, NewStorageEventArgs e) מייצג delegate לטיפול באירועים שמתרחשים לאחר יצירת קובץ אחסון חדש או לאחר עיבודו.

MimeItemCopyEventHandler(Object sender, MimeItemCopyEventArgs e) מייצג delegate לטיפול באירועים הקשורים להעתקת פריטי Mime, בדרך כלל בשימוש בתרחישים שבהם אובייקט MailMessage מוחלף מאחסון אחד לאחר.

NewStorageEventArgs מייצג ארגומנטים המשמשים באירועים שמתרחשים לאחר יצירת קובץ אחסון חדש או לאחר עיבודו.

MimeItemCopyEventArgs מייצג ארגומנטים של אירוע הקשורים להעתקת אובייקט MailMessage מאחסון אחד לאחר, לפני תחילת ההעתקה או אחרי סיומה.

קוד הדוגמה למטה מציג כיצד לתקשר עם קובצי Mbox, לטפל באירועים הקשורים לקבצים אלה, ולבצע פעולות כגון פיצול אחסון Mbox לחלקים קטנים יותר תוך שמירה על ספירת הודעות וספירת חלקים.

messageCount = 0;
partCount = 0;

// Create an instance of MboxrdStorageReader
MboxLoadOptions lo = new MboxLoadOptions();
lo.setLeaveOpen(false);
MboxrdStorageReader mbox = new MboxrdStorageReader("my.mbox", lo);

// Subscribe to events
mbox.setMboxFileCreatedEventHandler(new NewStorageEventHandler() {
    public void invoke(Object sender, NewStorageEventArgs e) {
        System.out.println("New Mbox file created: " + e.getFileName());
        partCount++;
    }
});

mbox.setMboxFileFilledEventHandler(new NewStorageEventHandler() {
    public void invoke(Object sender, NewStorageEventArgs e) {
        System.out.println("Mbox file filled with messages: " + e.getFileName());
    }
});

mbox.setEmlCopiedEventHandler(new MimeItemCopyEventHandler() {
    public void invoke(Object sender, MimeItemCopyEventArgs e) {
        System.out.println("Message added to new Mbox file. Subject: " + e.getItem().getSubject());
        messageCount++;
    }
});

// Split the Mbox storage into smaller parts
mbox.splitInto(10000000, testOutPath, "Prefix");

// Output the final messageCount and partCount
System.out.println("Total messages added: " + messageCount);
System.out.println("Total parts created: " + partCount);

המרת MBOX ל‑PST

כאשר ממגרים נתוני דואר מלקוחות כמו Mozilla Thunderbird ל‑Microsoft Outlook, לעיתים נדרש להמיר ארכיוני MBOX לפורמט PST (Personal Storage Table). פורמט PST הוא ייעודי ל‑Outlook ול‑Exchange ותומך בתכונות מתקדמות כגון מבנה תיקיות ועיבוד הודעות מבוסס MAPI.

המרה בסיסית

Aspose.Email עבור Java מאפשרת למפתחים להעביר הודעות מקבצי MBOX לאחסון תואם Outlook עם שליטה מלאה בתהליך ההמרה:

  1. אתחל את קורא ה‑MBOX באמצעות MboxStorageReader.createReader().
  2. צור קובץ PST באמצעות PersonalStorage.create().
  3. הוסיפו תיקיית דואר (למשל, "Inbox") לאחסון.
  4. עבור על כל הודעה, המר אותה ל- MapiMessage, והכנס אותה לתיקייה.
String mboxFilePath = "input.mbox";
String pstFilePath = "output.pst";

// Load the source file
try (MboxStorageReader mbox = MboxStorageReader.createReader(mboxFilePath, new MboxLoadOptions());
     // Create the destination Outlook data file
     PersonalStorage personalStorage = PersonalStorage.create(pstFilePath, FileFormatVersion.Unicode)) {

    // Add a folder to hold the imported messages
    FolderInfo folderInfo = personalStorage.createPredefinedFolder("Inbox", StandardIpmFolder.Inbox);

    // Process each message from the source file
    for (MailMessage eml : mbox.enumerateMessages()) {
        MapiMessage msg = MapiMessage.fromMailMessage(eml);
        folderInfo.addMessage(msg);
    }
}

המרה עם MailStorageConverter

MailStorageConverter מבצעת את אותה ההגירה בקריאה אחת, ויוצרת את תיקיית היעד עבורכם:

try (PersonalStorage pst = MailStorageConverter.mboxToPst("input.mbox", "output.pst",
        new MboxToPstConversionOptions())) {
    System.out.println("Conversion completed.");
}

הסרת חתימות דיגיטליות במהלך ההמרה

במקרים מסוימים, חתימות דיגיטליות המוטמעות בהודעות אינן נדרשות בקובץ היעד. כדי להוציא אותן במהלך ההעברה, קבע את RemoveSignature מאפיין של MboxToPstConversionOptions ל-true.

דוגמת הקוד הבא מראה כיצד להשתמש במאפיין זה:

MboxToPstConversionOptions options = new MboxToPstConversionOptions();
options.setRemoveSignature(true);

ByteArrayOutputStream pstDataStream = new ByteArrayOutputStream();

try (InputStream mboxStream = Files.newInputStream(Paths.get("input.mbox"));
     MboxrdStorageReader reader = new MboxrdStorageReader(mboxStream, new MboxLoadOptions());
     PersonalStorage personalStorage = PersonalStorage.create(pstDataStream, FileFormatVersion.Unicode)) {

    MailStorageConverter.mboxToPst(reader, personalStorage, "Inbox", options);
}