उपयोगिता सुविधाएँ - MailMessage

Contents
[ ]

संदेशों को एन्क्रिप्ट करना और डिक्रिप्ट करना

Aspose.Email ईमेल संदेशों को एन्क्रिप्ट और डिक्रिप्ट करने की सुविधा प्रदान करता है। यह विषय दर्शाता है कि मौजूदा या नया संदेश कैसे लोड कर एन्क्रिप्ट किया जा सकता है, उपयोग करके MailMessage. यह encrypt() और decrypt() मेथड्स लागू प्रभावों के लिए MailMessage ऑब्जेक्ट लौटाते हैं और संदेशों को एन्क्रिप्ट/डिक्रिप्ट करते समय इसका ध्यान रखना आवश्यक है। संदेशों को एन्क्रिप्ट और डिक्रिप्ट करने में निम्नलिखित कदम शामिल हैं:

  1. एक नया संदेश बनाएँ या मौजूदा को लोड करें
  2. सर्टिफिकेट फ़ाइल का उपयोग करके संदेश को एन्क्रिप्ट करें
  3. संदेश भेजें या सहेजें
  4. आवश्यकतानुसार संदेश को डिक्रिप्ट करें

निम्नलिखित कोड स्निपेट दिखाता है कि आप संदेशों को एन्क्रिप्ट और डिक्रिप्ट कैसे कर सकते हैं।

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

Path publicCertFilePath = Paths.get(dataDir + "MyKey.cer");
Path privateCertFilePath = Paths.get(dataDir + "MyPFX.pfx");

// Create a message
MailMessage msg = new MailMessage("sender@domain.com", "receiver@domain.com", "Test subject", "Test Body");

try {
    // Encrypt the message with the public certificate
    MailMessage encrypted = msg.encrypt(Files.readAllBytes(publicCertFilePath), "");
    System.out.println("Encrypted: " + encrypted.isEncrypted());

    // Decrypt it back with the private key and its password
    MailMessage decrypted = encrypted.decrypt(Files.readAllBytes(privateCertFilePath), "password");
    System.out.println("Encrypted: " + decrypted.isEncrypted());
} catch (IOException e) {
    e.printStackTrace();
}

संदेश की एन्क्रिप्शन जाँच

Aspose.Email MailMessage क्लास यह जाँचने की अनुमति देता है कि कोई संदेश एन्क्रिप्टेड है या नहीं। यह isEncrypted MailMessage की प्रॉपर्टी आपको यह जांचने की अनुमति देती है जैसा कि निम्न कोड नमूने में दिखाया गया है।

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

Path publicCertFilePath = Paths.get(dataDir + "MyKey.cer");
Path privateCertFilePath = Paths.get(dataDir + "MyPFX.pfx");

// Load a message and check whether it is already encrypted
MailMessage originalMsg = MailMessage.load(dataDir + "Message.msg", new MsgLoadOptions());
System.out.println("Encrypted: " + originalMsg.isEncrypted());

try {
    // Encrypt the message
    MailMessage mailMsg = originalMsg.encrypt(Files.readAllBytes(publicCertFilePath), "");
    System.out.println("Encrypted: " + mailMsg.isEncrypted());

    // Decrypt the message
    mailMsg = mailMsg.decrypt(Files.readAllBytes(privateCertFilePath), "password");
    System.out.println("Encrypted: " + mailMsg.isEncrypted());
} catch (IOException e) {
    e.printStackTrace();
}

S/MIME कुंजियों के साथ संदेशों को साइन और एन्क्रिप्ट करना

Aspose.Email for Java एक प्रमाणपत्र को, उसके निजी कुंजी के साथ या बिना, एक SmimeKey. वहां कोई X509Certificate2 जावा API में प्रकार: एक कुंजी PFX, निजी कुंजी फ़ाइल या प्रमाणपत्र फ़ाइल से SmimeKey की स्थैतिक फ़ैक्ट्री मेथड्स से लोड की जाती है, और वह ऑब्जेक्ट फिर साइनिंग और एन्क्रिप्शन मेथड्स को पास किया जाता है MailMessage:

String dataDir = "data/";

MailMessage msg = new MailMessage("sender@domain.com", "receiver@domain.com", "Signed", "Body");

// A PFX holds the private key and its certificate; the password is a char array
SmimeKey key = SmimeKey.loadPFX(dataDir + "MyPFX.pfx", "password".toCharArray());

// Sign the message - pass true for a detached signature, or use attachDetachedSignature
MailMessage signed = msg.attachSignature(key);

// Encrypt with one or more recipient certificates
SmimeKey recipientCert = SmimeKey.loadCertificate(dataDir + "MyKey.cer");
MailMessage encrypted = signed.encrypt(recipientCert);

// Decrypt with the private key
MailMessage decrypted = encrypted.decrypt(key);

// Inspect the certificates of a signed message
for (java.security.cert.X509Certificate certificate : decrypted.checkSignatureCert()) {
    System.out.println(certificate.getSubjectX500Principal());
}

Aspose.Email के लिए लोकेल विकल्प कॉन्फ़िगर करें

आप उपयोग कर सकते हैं LocaleOptions डिफ़ॉल्ट लोकेल पहचाना नहीं जाता तो क्लास, और Aspose.Email लाइब्रेरी के लिए सबसे उपयुक्त लोकेल सेट करता है। यह निम्नलिखित मेथड्स प्रदान करता है:

  • getLocale() - Aspose.Email द्वारा उपयोग की जाने वाली डिफ़ॉल्ट लोकेल लौटाता है।
  • setLocale(Locale locale) और setLocale(String localeName) - Aspose.Email द्वारा उपयोग की जाने वाली डिफ़ॉल्ट लोकेल सेट करता है।
  • clear() - Aspose.Email की डिफ़ॉल्ट लोकेल को साफ़ करता है, ताकि जावा की डिफ़ॉल्ट लोकेल फिर से उपयोग हो।

निम्नलिखित कोड नमूना दर्शाता है कि निर्दिष्ट लोकेल सेटिंग्स का उपयोग करके फ़ाइल से मेल संदेश कैसे लोड किया जाए:

final Locale locale = new Locale("en", "DE");
Locale.setDefault(locale);

// set Locale for Aspose Email lib
LocaleOptions.setLocale("en-US");
// or
//LocaleOptions.setLocale(new Locale("en", "US"));

MailMessage.load("document.msg");

यह कोड सुनिश्चित करता है कि एप्लिकेशन और Aspose.Email लाइब्रेरी भाषा, देश और सांस्कृतिक मानदंडों को संभालने के लिए निर्दिष्ट लोकेल्स का उपयोग करें।

TNEF अटैचमेंट वाले MailMessages

Transport Neutral Encapsulation Format (TNEF) माइक्रोसॉफ्ट आउटलुक और माइक्रोसॉफ्ट एक्सचेंज सर्वर द्वारा उपयोग किया जाने वाला एक स्वामित्व ईमेल अटैचमेंट फॉर्मेट है। Aspose.Email API आपको TNEF अटैचमेंट वाले ईमेल संदेश पढ़ने और उनकी सामग्री संशोधित करने की अनुमति देता है। ईमेल को फिर सामान्य ईमेल के रूप में या उसी फॉर्मेट में सहेजा जा सकता है, जिससे TNEF अटैचमेंट संरक्षित रहते हैं। यह लेख TNEF अटैचमेंट वाले संदेशों के साथ काम करने के विभिन्न कोड नमूने दिखाता है।

TNEF अटैचमेंट्स को संरक्षित रखते हुए संदेश पढ़ना

निम्नलिखित कोड स्निपेट दिखाता है कि आप TNEF अटैचमेंट्स को संरक्षित रखते हुए संदेश को कैसे पढ़ सकते हैं।

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

EmlLoadOptions options = new EmlLoadOptions();

// This preserves the TNEF attachment as it is, if the file contains one
options.setPreserveTnefAttachments(true);

MailMessage eml = MailMessage.load(dataDir + "tnefEml.eml", options);

for (Attachment attachment : eml.getAttachments()) {
    System.out.println(attachment.getName());
}

TNEF अटैचमेंट में रिसोर्सेज़ को अपडेट करना और TNEF फॉर्मेट को संरक्षित रखना

निम्नलिखित कोड स्निपेट दिखाता है कि आप TNEF अटैचमेंट में रिसोर्सेज़ को कैसे अपडेट करें और TNEF फॉर्मेट को संरक्षित रखें।

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void updateTnefResources(String dataDir) throws Exception {
    String fileName = dataDir + "tnefEml.eml";
    String imgFileName = dataDir + "Untitled.jpg";
    String outFileName = dataDir + "01_SAVE_Preserve_out.eml";

    MailMessage originalMailMessage = MailMessage.load(fileName);
    updateResources(originalMailMessage, imgFileName);

    EmlSaveOptions emlSaveOptions = new EmlSaveOptions(MailMessageSaveType.getEmlFormat());
    emlSaveOptions.setFileCompatibilityMode(FileCompatibilityMode.PreserveTnefAttachments);

    originalMailMessage.save(outFileName, emlSaveOptions);
}

private static void updateResources(MailMessage msg, String imgFileName) throws Exception {
    for (int i = 0; i < msg.getAttachments().size(); i++) {
        Attachment attachment = msg.getAttachments().get_Item(i);
        String name = attachment.getContentType().getName();

        if (name.endsWith("jpg")) {
            attachment.setContentStream(new FileInputStream(imgFileName));
        } else if (name.endsWith("eml")) {
            // Replace the resources of the embedded message, then put it back
            ByteArrayOutputStream embedded = new ByteArrayOutputStream();
            attachment.save(embedded);

            MailMessage embeddedMessage = MailMessage.load(new ByteArrayInputStream(embedded.toByteArray()));
            updateResources(embeddedMessage, imgFileName);

            ByteArrayOutputStream processed = new ByteArrayOutputStream();
            embeddedMessage.save(processed, SaveOptions.getDefaultMsgUnicode());

            attachment.setContentStream(new ByteArrayInputStream(processed.toByteArray()));
        }
    }

    for (LinkedResource resource : msg.getLinkedResources()) {
        // Compare the media type with equals: == compares references, not the text
        if ("image/jpeg".equals(resource.getContentType().getMediaType())) {
            resource.setContentStream(new FileInputStream(imgFileName));
        }
    }
}

TNEF वाले मुख्य संदेश में नए अटैचमेंट जोड़ना

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

String fileName = "MainMessage.eml";
String attachName = "barcode.png";
String outFileName = "test_out.eml";

MailMessage eml = MailMessage.load(dataDir + fileName);

try (FileInputStream fi = new FileInputStream(dataDir + attachName)) {
    eml.getAttachments().addItem(new Attachment(fi, attachName, "image/png"));
    eml.save(dataDir + outFileName);
}

MSG से TNEF EML बनाना

Outlook MSG कभी-कभी तालिकाओं और टेक्स्ट स्टाइल जैसी जानकारी रखते हैं जो यदि EML में परिवर्तित किया जाए तो बिगड़ सकते हैं। ऐसे MSG फ़ाइलों से TNEF संदेश बनाना हमें फॉर्मेटिंग को बरकरार रखने और यहां तक कि ईमेल क्लाइंट्स के ज़रिए फॉर्मेटिंग को बनाए रखते हुए संदेश भेजने की अनुमति देता है।

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MapiMessage msg = MapiMessage.load(dataDir + "Message.msg");

MailConversionOptions options = new MailConversionOptions();
options.setConvertAsTnef(true);

MailMessage mail = msg.toMailMessage(options);

TNEF बनाने के लिए, निम्नलिखित सैंपल कोड का उपयोग किया जा सकता है।

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MsgLoadOptions msgLoadOptions = new MsgLoadOptions();

// The PreserveTnefAttachments option creates a TNEF EML out of an MSG file
msgLoadOptions.setPreserveTnefAttachments(true);

MailMessage eml = MailMessage.load(dataDir + "Message.msg", msgLoadOptions);

जांचें कि संदेश TNEF है या नहीं

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MailMessage mail = MailMessage.load(dataDir + "test.eml");

boolean isTnef = mail.getOriginalIsTnef();
System.out.println("isTnef: " + isTnef);

बाउंस किए गए संदेशों को प्रोसेस करना

किसी कारण से, जैसे कि अमान्य प्राप्तकर्ता पता, प्राप्तकर्ता को भेजा गया संदेश बाउंस हो सकता है। Aspose.Email API के पास ऐसे संदेश को यह जांचने के लिए क्षमता है कि वह बाउंस किया गया ईमेल है या सामान्य ईमेल संदेश। checkBounced विधि MailMessage क्लास एक वैध परिणाम लौटाता है यदि ईमेल संदेश बाउंस्ड ईमेल हो।

यह लेख दिखाता है कि BounceResult क्लास, जो दर्शाती है कि क्या संदेश बाउंस्ड ईमेल है और प्राप्तकर्ताओं, ली गई कार्रवाई और नोटिफिकेशन के कारण के बारे में विस्तृत जानकारी देती है।

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the resource directory.
String dataDir = "data/";

for (String fileName : new String[] { "failed.msg", "failed1.msg", "test.eml" }) {
    MailMessage mail = MailMessage.load(dataDir + fileName);
    BounceResult result = mail.checkBounced();

    System.out.println(fileName);
    System.out.println("IsBounced : " + result.isBounced());
    System.out.println("Action : " + result.getAction());
    System.out.println("Recipient : " + result.getRecipient());

    if (result.isBounced()) {
        System.out.println("Reason : " + result.getReason());
        System.out.println("Status : " + result.getStatus());
        System.out.println("OriginalMessage ToAddress 1: "
                + result.getOriginalMessage().getTo().get_Item(0).getAddress());
    }
    System.out.println();
}

एक्सेप्शन अनदेखा करें

यह लाइब्रेरी एक ExceptionManager ऐसी क्लास जिससे आपका एप्लिकेशन चयनित एक्सेप्शन को अनदेखा कर सके, सभी को अनदेखा कर सके, या उनपर लॉग रख सके जिन्हें वह अनदेखा करता है। देखें अपवादों को कैसे अनदेखा करें विवरण और कोड नमूने के लिए।

बेयेसियन स्पैम विश्लेषक

Aspose.Email बायेसियन स्पैम विश्लेषक का उपयोग करके ईमेल फ़िल्टरिंग की सुविधा प्रदान करता है। यह प्रदान करता है SpamAnalyzer इस उद्देश्य के लिए क्लास। यह लेख दिखाता है कि कैसे शब्द डेटाबेस का उपयोग करके फ़िल्टर को स्पै्म और सामान्य ईमेल में अंतर करने के लिए प्रशिक्षित किया जाए।

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void spamFilterTest(String dataDir) {
    String hamFolder = dataDir + "ham";
    String spamFolder = dataDir + "spam";
    String testFolder = dataDir + "test";
    String databaseFile = dataDir + "SpamFilterDatabase.txt";

    teachAndCreateDatabase(hamFolder, spamFolder, databaseFile);

    SpamAnalyzer analyzer = new SpamAnalyzer(databaseFile);

    for (File testFile : new File(testFolder).listFiles()) {
        MailMessage msg = MailMessage.load(testFile.getAbsolutePath());
        System.out.println(msg.getSubject());
        printResult(analyzer.test(msg));
    }
}

private static void teachAndCreateDatabase(String hamFolder, String spamFolder, String databaseFile) {
    SpamAnalyzer analyzer = new SpamAnalyzer();

    // Train on the known-good messages, then on the known-spam ones
    train(analyzer, new File(hamFolder).listFiles(), false);
    train(analyzer, new File(spamFolder).listFiles(), true);

    analyzer.saveDatabase(databaseFile);
}

private static void train(SpamAnalyzer analyzer, File[] files, boolean isSpam) {
    for (File file : files) {
        try {
            analyzer.trainFilter(MailMessage.load(file.getAbsolutePath()), isSpam);
        } catch (RuntimeException e) {
            // Skip the files that cannot be loaded as messages
        }
    }
}

private static void printResult(double probability) {
    if (probability < 0.05) {
        System.out.println("This is ham");
    } else if (probability > 0.95) {
        System.out.println("This is spam");
    } else {
        System.out.println("Maybe spam");
    }
}

EML संदेशों से प्रीएम्बल और उपसंहार प्राप्त करना

MIME फ़ॉर्मेट में, प्रीएम्बल वह टेक्स्ट है जो हेडर के बाद और पहले मल्टीपार्ट बाउंड्री से पहले दिखाई देता है। एपिलॉग वह टेक्स्ट है जो अंतिम बाउंड्री के बाद और संदेश के अंत से पहले आता है। यह टेक्स्ट आमतौर पर मेल रीड़र में उपयोगकर्ताओं को दिखाई नहीं देता, लेकिन कुछ MIME इम्प्लीमेंटेशन इसे उन प्राप्तकर्ताओं के लिए नोट्स डालने हेतु उपयोग करते हैं जो गैर‑MIME‑अनुपालनीय प्रोग्राम से संदेश पढ़ते हैं।

प्रीएम्बल और एपिलॉग को संबंधित मेथड्स के माध्यम से पढ़ा और लिखा जाता है MailMessage क्लास:

String dataDir = "data/";

MailMessage eml = MailMessage.load(dataDir + "message.eml");

// Read the preamble and the epilogue of the loaded message
System.out.println("Preamble: " + eml.getPreamble());
System.out.println("Epilogue: " + eml.getEpilogue());

// Both can be set before saving the message again
eml.setPreamble("This is a multi-part message in MIME format.");
eml.setEpilogue("End of the MIME message.");

eml.save(dataDir + "message_out.eml", SaveOptions.getDefaultEml());

MHT फ़ाइलों में वैकल्पिक प्रतिभागियों को दिखाएँ

MHT फ़ॉर्मेट पर काम करते समय, आप कैलेंडर इवेंट के हेडर में वैकल्पिक प्रतिभागियों की जानकारी को दिखा या छिपा सकते हैं। कॉन्फ़िगर करने के लिए MhtSaveOptions MHT फ़ाइल हैंडलिंग के लिए, आपको समझना होगा कि MhtFormatOptions.RenderCalendarEvent और MhtFormatOptions.WriteHeader मूल्य कार्य करते हैं:

  • MhtFormatOptions.RenderCalendarEvent - नियंत्रित करता है कि कैलेंडर इवेंट के विवरण MHT फ़ाइल में रेंडर किए जाएँ या नहीं। इस विकल्प को सेट करके आप सुनिश्चित करते हैं कि विस्तृत इवेंट जानकारी, जिसमें प्रतिभागी विवरण शामिल हैं, आउटपुट में सम्मिलित हो।
  • MhtFormatOptions.WriteHeader - निर्धारित करता है कि विषय, तिथि, और प्रतिभागी जानकारी (वैकल्पिक प्रतिभागियों सहित, यदि कॉन्फ़िगर किए गए) जैसे मेटाडेटा वाले हेडर MHT फ़ाइल में लिखे जाएँ या नहीं।

नीचे दिया गया कोड नमूना दिखाता है कि वैकल्पिक प्रतिभागियों को दिखाएं सुविधा का उपयोग करके संदेश को MHTML फ़ॉर्मेट में कैसे सहेजा जाए:

MhtSaveOptions options = new MhtSaveOptions();
options.setMhtFormatOptions(MhtFormatOptions.RenderCalendarEvent | MhtFormatOptions.WriteHeader);

MailMessage eml = MailMessage.load("meeting.msg");
eml.save("meeting.mhtml", options);

यदि आप MHT फ़ाइल में वैकल्पिक प्रतिभागियों की जानकारी को बाहर रखना चाहते हैं, तो सहेजने से पहले वैकल्पिक प्रतिभागियों के लिए फ़ॉर्मेट टेम्प्लेट साफ़ करें:

MhtSaveOptions options = new MhtSaveOptions();
options.setMhtFormatOptions(MhtFormatOptions.RenderCalendarEvent | MhtFormatOptions.WriteHeader);

// Skip the optional attendees in the MHTML output
options.getFormatTemplates().set_Item(MhtTemplateName.OPTIONAL_ATTENDEES, "");

MailMessage eml = MailMessage.load("meeting.msg");
eml.save("meeting2.mhtml", options);

MHTML में सभी हेडर सहेजें

यह SaveAllHeaders की प्रॉपर्टी MhtSaveOptions क्लास निर्धारित करता है कि सभी हेडर आउटपुट MHTML में लिखे जाएँ या नहीं। निम्नलिखित कोड स्निपेट दिखाता है कि सभी हेडर को MHTML फ़ाइल में कैसे सहेजा जाए:

MailMessage eml = MailMessage.load("message.eml");

MhtSaveOptions sopt = SaveOptions.getDefaultMhtml();
sopt.setSaveAllHeaders(true);

eml.save("message.mhtml", sopt);

MDN और रीड रसीदों का उपयोग करके ईमेल ट्रैकिंग

Aspose.Email API संदेश डिस्पोज़िशन नोटिफिकेशन (MDN) के द्वारा ई‑मेल ट्रैकिंग को समर्थन देता है। यह पढ़ने की रसीदों का अनुरोध करके और आवश्यक जानकारी बनाकर प्राप्त किया जाता है:

  • MailMessage.setReadReceiptTo(MailAddressCollection) पढ़ने की रसीद पतों को सेट करता है।
  • MailMessage.createReadReceipt(String, String) प्राप्त संदेश के लिए पढ़ने की रसीद बनाता है।
  • MapiMessage.setReadReceiptRequested(boolean) निर्धारित करता है कि Outlook संदेश के लिए पढ़ने की रसीद अनुरोध की गई है या नहीं।

निम्नलिखित कोड स्निपेट दिखाता है कि Aspose.Email API का उपयोग करके ई‑मेल ट्रैक कैसे करें:

try (SmtpClient client = new SmtpClient("smtp.server.com", 587, "username", "password")) {

    // Send a message with a requested read receipt
    MailMessage mailMessage = new MailMessage("from@domain.com", "to@domain.com",
            "test MDN", "This is a test message with read receipt requested");

    MailAddressCollection readReceiptTo = new MailAddressCollection();
    readReceiptTo.add("from@domain.com");
    readReceiptTo.add("another@domain.com");
    mailMessage.setReadReceiptTo(readReceiptTo);

    client.send(mailMessage);

    // On the recipient side: check the request, then create and send the read receipt
    MailMessage emlMDN = MailMessage.load("received.eml");
    if (emlMDN.getReadReceiptTo().size() > 0) {
        client.send(emlMDN.createReadReceipt("to@domain.com", null));
    }
}

एक पढ़ने की रसीद भी Outlook संदेश पर अनुरोध की जा सकती है:

// Create a MapiMessage with a requested read receipt
MapiMessage mapiMessage = new MapiMessage("from@domain.com", "to@domain.com",
        "test MDN", "This is a read requested mapiMessage", OutlookMessageFormat.Unicode);
mapiMessage.setReadReceiptRequested(true);

// Or create a MailMessage with a requested read receipt and convert it to a MapiMessage
MailMessage mailMessage = new MailMessage("from@domain.com", "to@domain.com",
        "test MDN", "This is a test message with read receipt requested");

MailAddressCollection readReceiptTo = new MailAddressCollection();
readReceiptTo.add("from@domain.com");
mailMessage.setReadReceiptTo(readReceiptTo);

MapiMessage converted = MapiMessage.fromMailMessage(mailMessage, MapiConversionOptions.getUnicodeFormat());