ایجاد یک فایل پیام Outlook (MSG)

اتوماسیون Office

برای استفاده از اشیاء Office Automation برای Microsoft Outlook، 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 استفاده می‌کنند. این کتابخانه به‌صورت خالص جاوا نوشته شده و از 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());