Een Outlook‑bericht (MSG)‑bestand maken
Onze migratietips laten zien hoe Aspose-producten kunnen worden gebruikt om je applicaties te verbeteren en je te bevrijden van afhankelijkheid van traditionele automatisering.
Deze migratietip toont hoe een Outlook‑Message (MSG)‑bestand te maken met behulp van Microsoft Office‑automatisering en Aspose.Email. De code‑voorbeelden stellen de basiseigenschappen van het MSG‑bestand in – Aan, CC, Onderwerp en HTML‑inhoud – voordat het bestand op schijf wordt opgeslagen.
Office‑automatisering
Om Office Automation te gebruiken, moet Microsoft Outlook geïnstalleerd zijn op de machine waarop de code wordt uitgevoerd. Een verwijzing naar Outlook.interop.dll is ook vereist.
Programmeer Voorbeelden
De volgende code‑fragmenten maken een MSG‑bestand met behulp van 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 voor Java
De onderstaande voorbeelden gebruiken Aspose.Email om het Outlook‑MSG‑bestand te maken. Het is geschreven in puur Java en maakt geen gebruik van COM‑Interop. Een Outlook‑installatie is niet vereist voor het op deze manier maken van het MSG‑bestand.
// 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());