MSG फ़ाइलें बनाना और सेव करना
Aspose.Email Outlook संदेश (MSG) फ़ाइलें बनाने का समर्थन करता है। यह लेख समझाता है कि कैसे:
- MSG संदेश बनाएं।
- अटैचमेंट के साथ MSG संदेश बनाएं।
- MapiMessage में प्राप्तकर्ताओं और अटैचमेंट को जोड़ें और जोड़ने के तुरंत बाद उन्हें कॉन्फ़िगर करें।
- 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);
MapiMessage में प्राप्तकर्ताओं और अटैचमेंट जोड़ना
A MapiMessage को सीधे भी भरा जा सकता है, बिना किसी को बदलें MailMessage. यह add के मेथड्स MapiRecipientCollection और MapiAttachmentCollection क्लासेस बनाए गए ऑब्जेक्ट को लौटाते हैं, इसलिए आप नया प्राप्तकर्ता या अटैचमेंट तुरंत कॉन्फ़िगर कर सकते हैं, बिना संग्रह में अलग से लुकअप किए:
- MapiRecipientCollection.add(String address, String displayName, int recipientType) और add(String address, String addressType, String displayName, int recipientType) बनाए गए को लौटाते हैं MapiRecipient.
- MapiAttachmentCollection.add(String name, byte[] data) और अन्य
addओवरलोड्स बनाए गए MapiAttachment.
प्राप्तकर्ता प्रकार में से एक है MapiRecipientType निम्नलिखित कोड स्निपेट एक प्राप्तकर्ता और एक अटैचमेंट जोड़ता है और फिर उनके डिस्प्ले नाम बदलता है:
MapiMessage message = new MapiMessage();
message.setSubject("Invoice");
// Add a recipient and configure it
MapiRecipient recipient = message.getRecipients().add("alice.johnson@example.com", "SMTP", "Alice Johnson", MapiRecipientType.MAPI_TO);
recipient.setDisplayName("Alice Johnson (Accounting)");
// Add an attachment from file bytes and configure it
MapiAttachment attachment = message.getAttachments().add("invoice.pdf", Files.readAllBytes(Paths.get("invoice.pdf")));
attachment.setDisplayName("Invoice #2026-001.pdf");
message.save("invoice.msg");
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);