Outlook MSG फ़ाइल में अटैचमेंट्स और HTML टेक्स्ट जोड़ना

Office Automation

इस विधि का उपयोग करने पर, VBA कोड चलाने वाली मशीन पर Microsoft Outlook स्थापित होना चाहिए। नीचे दिया गया कोड स्निपेट अटैचमेंट्स और HTML बॉडी के साथ Outlook MSG फ़ाइल बनाता है।

VBA


 ' Create an object of type Outlook.Application

Set objOutlookApplication = CreateObject("Outlook.Application")

' Create an object of type olMailItem

Set objMsg = objOutlookApplication.CreateItem(olMailItem)

' Set properties of the message file e.g. subject, body and to address

' Set subject

objMsg.Subject = "This MSG file is created using Office Automation."

' Set to (recipient) address

objMsg.To = "to@domain.com"

' Set body of the email message

objMsg.HTMLBody = "<html><p>This MSG file is created using VBA code.</p>"

' Add attachments to the message

objMsg.Attachments.Add "C:\test.bmp"

objMsg.Attachments.Add "C:\test2.jpg"

' Save as Outlook MSG file

objMsg.SaveAs ("c:\testvba.msg")

' Open the MSG file

objMsg.Display

Aspose.Email for Java

नीचे का कोड स्निपेट Aspose.Email for Java लाइब्रेरी का उपयोग करके MSG फ़ाइल बनाता है, जो समान है उपर्युक्त बनाई गई, कई अटैचमेंट्स और HTML बॉडी के साथ। क्योंकि Aspose.Email for Java पूरी तरह से Java में लिखा गया है, COM इंटरऑप की आवश्यकता नहीं है। साथ ही Microsoft Outlook 2003/2007 को मशीन पर स्थापित करने की ज़रूरत नहीं है। नीचे वर्णित विधि तब उपयुक्त है जब Microsoft Outlook स्थापित न हो या आप सर्वर पर MSG फ़ाइलें जनरेट करना चाहते हों।

नीचे दिए गए कोड स्निपेट्स दिखाते हैं कि कैसे Java में Aspose.Email for Java का उपयोग करके उसी कार्य को किया जा सकता है:


// Create an instance of type MailMessage
MailMessage msg = new MailMessage();

// Set properties of message like subject, to and HTML body
// Set subject
msg.setSubject("This MSG file is created using Aspose.Email for .NET");

// Set from (sender) address
msg.setSender(new MailAddress("from@domain.com", "From Name"));

// Set to (recipient) address and name
msg.getTo().addItem(new MailAddress("to@domain.com", "To Name"));

// Set HTML body of the email message
msg.setHtmlBody("<html><p>This MSG file is created using Java code.</p>" 
        + "<p>Microsoft Outlook does not need to be installed on the machine running this code.</p>"
        + "<p>This method is suitable for creating MSG files on the server side.</html>");

// Add attachments to the message file
msg.getAttachments().addItem(new Attachment("C:\\test.bmp"));
msg.getAttachments().addItem(new Attachment("C:\\test2.jpg"));

// Save as Outlook MSG file
String strSaveFile = "C:\\TestAspose.msg";

msg.save(strSaveFile, SaveOptions.getDefaultMsgUnicode());