יצירת הודעת Outlook (MSG)
טיפי המיגרציה שלנו מראות כיצד ניתן להשתמש במוצרים של Aspose כדי לשפר את היישומים שלכם ולשחרר אתכם מהתלות באוטומציה מסורתית.
טיפ מיגרציה זה מראה כיצד ליצור קובץ הודעת Outlook (MSG) באמצעות אוטומציית Microsoft Office ו Aspose.Email. דוגמאות הקוד מגדירות את המאפיינים הבסיסיים של קובץ MSG - To, Cc, Subject וגוף HTML - לפני שמירת הקובץ על הדיסק.
אוטומציית Office
כדי להשתמש באוטומציית Office, יש להתקין את Microsoft Outlook במחשב שעליו רץ הקוד. נדרשת גם הפנייה ל‑Outlook.interop.dll.
דוגמאות תכנות
קטעי הקוד הבאים יוצרים קובץ MSG באמצעות Office Automation.
C#
// Creates a new Outlook Application instance
Outlook.Application objOutlook = new Outlook.Application();
// Creating a new Outlook message from the Outlook Application instance
Outlook.MailItem msgInterop = (Outlook.MailItem)(objOutlook.CreateItem(Outlook.OlItemType.olMailItem));
// Set recipient information
msgInterop.To = "to@domain.com";
msgInterop.CC = "cc@domain.com";
// Set the message subject
msgInterop.Subject = "Subject";
// Set some HTML text in the HTML body
msgInterop.HTMLBody = "<h3>HTML Heading 3</h3> <u>This is underlined text</u>";
// Save the MSG file in local disk
string strMsg = @"c:\\temp\TestInterop.msg";
msgInterop.SaveAs(strMsg, Outlook.OlSaveAsType.olMSG);
Aspose.Email עבור Java
הדוגמאות למטה משתמשות ב‑Aspose.Email ליצירת קובץ Outlook MSG. הוא נכתב ב‑Java טהורה ולא משתמש ב‑COM Interop. אין צורך בהתקנת Outlook לצורך יצירת קובץ ה‑msg בצורה זו.
// Create an instance of the Aspose.Email.MailMessage class
MailMessage msg = new MailMessage();
// Set recipients information
msg.setTo(MailAddressCollection.to_MailAddressCollection("to@domain.com"));
msg.setCC(MailAddressCollection.to_MailAddressCollection("cc@domain.com"));
// Set the subject
msg.setSubject("Subject");
// Set HTML body
msg.setHtmlBody("<h3>HTML Heading 3</h3> <u>This is underlined text</u>");
// Add an attachment
msg.getAttachments().addItem(new Attachment("test.txt"));
// Save it in local disk
String strMsg = "c:\\ TestAspose.msg";
msg.save(strMsg, SaveOptions.getDefaultMsgUnicode());