Skapa en Outlook‑meddelandefil (MSG)
Våra migrationstips visar hur Aspose‑produkter kan användas för att förbättra dina applikationer och frigöra dig från beroende av traditionell automatisering.
Detta migrations‑tips visar hur man skapar en Outlook‑meddelandefil (MSG) med hjälp av Microsoft Office‑automation och Aspose.Email. Kodexemplen sätter MSG‑filens grundläggande egenskaper – Till, Kopia, Ämne och HTML‑kropp – innan filen sparas på disk.
Office‑automation
För att använda Office‑automation måste Microsoft Outlook vara installerat på den maskin där koden körs. En referens till Outlook.interop.dll krävs också.
Programmeringsexempel
Följande kodsnuttar skapar en MSG‑fil med 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 för Java
Exemplen nedan använder Aspose.Email för att skapa Outlook‑MSG‑filen. Den är skriven i ren Java och använder inte COM‑Interop. Outlook‑installation krävs inte för att skapa MSG‑filen på detta sätt.
// 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());