MSG फ़ाइलें बनाना और सेव करना
Aspose.Email Outlook संदेश (MSG) फ़ाइलें बनाने का समर्थन करता है। यह लेख समझाता है कि कैसे:
- MSG संदेश बनाएं।
- अटैचमेंट के साथ MSG संदेश बनाएं।
- RTF बॉडी के साथ MSG संदेश बनाएं।
- संदेश को ड्राफ्ट के रूप में सहेजें।
- बॉडी संपीड़न के साथ काम करें।
Outlook संदेश बनाना और सेव करना
यह MailMessage क्लास में है save विधि जो Outlook MSG फ़ाइलों को डिस्क या स्ट्रीम में सहेज सकती है। नीचे दिया गया कोड स्निपेट इस क्लास का एक उदाहरण बनाता है MailMessage क्लास, from, to, subject और body जैसी प्रॉपर्टीज़ सेट करें। यह save विधि फ़ाइल नाम को एक तर्क के रूप में लेती है। इसके अलावा, Outlook संदेशों को एक संपीड़ित RTF बॉडी के उपयोग से MapiConversionOptions.
- एक नया उदाहरण बनाएं MailMessage क्लास का और From, To, Subject तथा Body प्रॉपर्टीज़ सेट करे।
- कॉल करें MapiMessage क्लास fromMailMessage विधि जो ऑब्जेक्ट लेती है MailMessage टाइप। यह fromMailMessage विधि परिवर्तित करती है MailMessage में एक MapiMessage (MSG).
- कॉल करें MapiMessage.save MSG फ़ाइल को सहेजने की विधि।
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");
// Create an instance of the MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
// Save the message (MSG) file
String strMsgFile = "CreatingAndSavingOutlookMessages_out.msg";
outlookMsg.save(dataDir + strMsgFile);
अटैचमेंट वाले MSG फ़ाइलें बनाना
उपरोक्त उदाहरण में, हमने एक सरल MSG फ़ाइल बनाई। Aspose.Email अटैचमेंट वाले संदेश फ़ाइलों को सहेजने का भी समर्थन करता है। आपको केवल अटैचमेंट को MailMessage उदाहरण। अटैचमेंट जोड़ने के लिए addItem मेथड को कॉल करें MailMessage.Attachments संग्रह।
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
String[] files = new String[2];
files[0] = "attachment.doc";
files[1] = "attachment.png";
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");
// Add the attachments
for (String strFileName : files)
{
mailMsg.getAttachments().addItem(new Attachment(strFileName));
}
// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
String strMsgFile = "CreateMessagesWithAttachments.msg";
outlookMsg.save(dataDir + strMsgFile);
RTF बॉडी वाली MSG फ़ाइलें बनाना
आप Aspose.Email के साथ रिच टेक्स्ट (RTF) बॉडी वाली Outlook Message (MSG) फ़ाइलें भी बना सकते हैं। RTF बॉडी टेक्स्ट फ़ॉर्मेटिंग को समर्थन देती है। इसे बनाने के लिए सेट करें MailMessage.HtmlBody प्रॉपर्टी में। जब आप एक MailMessage उदाहरण को एक MapiMessage उदाहरण में, HTML बॉडी को RTF में परिवर्तित किया जाता है। इस तरह, ईमेल बॉडी की फ़ॉर्मेटिंग संरक्षित रहती है।
निम्न उदाहरण एक RTF बॉडी वाली MSG फ़ाइल बनाता है। HTML बॉडी में एक हेडिंग, बोल्ड और अंडरलाइन फ़ॉर्मेटिंग लागू है। जब HTML को RTF में बदलते हैं तो यह फ़ॉर्मेटिंग बरकरार रहती है।
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setHtmlBody("<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>");
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
outlookMsg.save(dataDir + "CreatingMSGFilesWithRTFBody_out.msg");
ड्राफ्ट स्थिति में संदेश सहेजना
जब कोई ईमेल संपादन शुरू करता है लेकिन बाद में पूरा करने के लिए वापस आना चाहता है, तो ईमेल ड्राफ्ट के रूप में सेव होते हैं। Aspose.Email एक संदेश फ़्लैग सेट करके ईमेल संदेश को ड्राफ्ट स्थिति में सहेजने का समर्थन करता है। नीचे Outlook ईमेल संदेश (MSG) को ड्राफ्ट के रूप में सहेजने का नमूना कोड दिया गया है।
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
// Change properties of an existing MSG file
String strExistingMsg = "message.msg";
// Load the existing file in MailMessage and Change the properties
MailMessage msg = MailMessage.load(dataDir + strExistingMsg, new MsgLoadOptions());
msg.setSubject(msg.getSubject() + " NEW SUBJECT (updated by Aspose.Email)");
msg.setHtmlBody(msg.getHtmlBody() + " NEW BODY (udpated by Aspose.Email)");
// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it
MapiMessage mapiMsg = MapiMessage.fromMailMessage(msg);
mapiMsg.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapiMsg.save(dataDir + "SavingMessageInDraftStatus_out.msg");
बॉडी संपीड़न के प्रभाव
RTF बॉडी संपीड़न विधि का उपयोग करके छोटे आकार की MSG उत्पन्न की जा सकती है। हालांकि, इससे निर्माण गति धीमी हो जाती है। संदेशों को तेज़ी से बनाने के लिए फ़्लैग को false पर सेट करें। यह फ़्लैग बदले में बनाए गए PST पर प्रभाव डालता है: छोटे MSG फ़ाइलें छोटे PST बनाती हैं, और बड़े MSG फ़ाइलें PST निर्माण को धीमा करती हैं।
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String fileName = "outlook/test.msg";
MailMessage message = MailMessage.load(fileName);
MapiConversionOptions options = new MapiConversionOptions();
options.setUseBodyCompression(true);
MapiMessage ae_mapi = MapiMessage.fromMailMessage(message, options);