Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Processing a multipart/report Delivery Status Notification (Bounce Notification)

See more Email Object Examples

This example discusses the format of Delivery Status Notification emails and how to process them.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Handle hoH2t
    Variant vSa
    Handle hoSa
    Integer iNumFinalRecipients
    Integer i
    String sHeadersText
    Handle hoMime
    Variant vOrigEmail
    Handle hoOrigEmail
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    // Here are the MIME structures, showing the content-type and nesting of the MIME parts of three sample
    // multipart/report DSN (Delivery Status Notification) emails.

    // This 1st sample includes a "text/rfc822-headers" MIME subpart. 

    // multipart/report
    //     text/plain
    //     message/delivery-status
    //     text/rfc822-headers
    //     message/rfc822

    // This 2nd sample lacks the text/rfc-headers part, but the "report type" information
    // is offered in both plain-text and HTML formats.

    // multipart/report
    //     multipart/alternative
    //         text/plain
    //         text/html
    //     message/delivery-status
    //     message/rfc822

    // ----------------------------------------------------------------------------------------------------

    // A multipart/report MIME delivery status notification follows a specific format defined by the Internet Engineering Task Force (IETF) in RFC 3464, 
    // which outlines the "An Extensible Message Format for Delivery Status Notifications" standard. The format consists of multiple parts within a 
    // multipart/report structure. Here's an overview of the main parts involved:
    // 
    // The 1st sub-part under multipart/report is the body of the DSN to be displayed by the email client (such as Outlook).
    // It can be a simple text/plain body, or it can be multipart/alternative and offer a few alternative format, typically plain-text and HTML.
    // HTML is best for viewing a program such as Outlook. 
    // This part of the multipart/report is not structured for programmatic processing.  It's meant to be viewed by a human.

    // --------------------
    // The "message/delivery-status" part within a multipart/report MIME structure follows a specific format to provide details about the delivery status
    // of an email message.  Here's an overview of the format and the key components within the "message/delivery-status" part:
    // 
    // (1) Content-Type and Reporting-UA:
    // The "message/delivery-status" part begins with the Content-Type header specifying "message/delivery-status". 
    // It may also include a Reporting-UA (Reporting User Agent) field that identifies the software or system generating the delivery status notification.
    // 
    // For example:
    // 
    // Content-Type: message/delivery-status
    // Reporting-UA: Example Mail System 1.0
    // 
    // (2) Fields:
    // The "message/delivery-status" part contains a series of fields, each providing specific information about the delivery status. 
    // These fields are structured as key-value pairs.
    // 
    // Common fields include:
    // 
    //     Final-Recipient: Specifies the recipient for whom the delivery status is being reported.
    //     Action: Describes the action performed by the reporting system (e.g., failed, delivered, delayed, etc.).
    //     Status: Indicates the status code or reason for the delivery attempt result.
    //     Remote-MTA: Specifies the host or system that attempted the delivery.
    //     Diagnostic-Code: Provides additional diagnostic information, such as error codes or explanations.
    // 
    // For example:
    // 
    // Final-Recipient: rfc822; john.doe@example.com
    // Action: failed
    // Status: 5.1.1
    // Remote-MTA: smtp.example.com
    // Diagnostic-Code: smtp; 550 Requested action not taken: mailbox unavailable
    // 
    // (3) Additional Fields:
    // Additional fields may be included in the "message/delivery-status" part to provide further information about the delivery attempt. 
    // These fields can vary depending on the implementation or specific needs of the system generating the delivery status notification.
    // For example:
    // 
    // X-Spam-Flag: YES
    // X-Spam-Score: 7.2
    // 
    // Note: The specific fields and their values within the "message/delivery-status" part can vary depending on the implementation
    // or the email server/application generating the delivery status notification. The structure described above represents the standard format
    // as defined in RFC 3464, but variations may exist in practice.

    // --------------------

    // The "text/rfc822-headers" MIME part, if included, contains the headers of the original email message
    // for which the delivery status notification is being generated. It provides a subset of the headers from the original message,
    // typically excluding the message body and attachments.
    // 
    // The purpose of including the "text/rfc822-headers" part is to provide contextual information about the original message.
    // It allows the recipient to review the original headers, such as the subject, sender, recipients, date, and other relevant information,
    // in order to understand the context and details of the email message for which the delivery status notification is being generated.
    // 
    // Note that the specific headers included in the "text/rfc822-headers" part can vary based on the implementation or requirements
    // of the system generating the delivery status notification.

    // --------------------

    // If the message/rfc822 part is present, it contains the full MIME of the email that was not delivered.
    // In Chilkat terminology, this is an attached message.

    // OK, let's write code to process a multipart/report email.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get ComLoadEml Of hoEmail "qa_data/eml/deliveryStatus.eml" To iSuccess
    // success = email.LoadEml("qa_data/eml/sample_multipart_report.eml");
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Verify this is a multipart/report email..
    Get ComIsMultipartReport Of hoEmail To bTemp1
    If (bTemp1 = False) Begin
        Showln "Not a multipart/report email."
        Procedure_Return
    End

    // Get the body that is to be displayed to a human in an email program (such as Outlook).
    Get ComHasPlainTextBody Of hoEmail To bTemp1
    If (bTemp1 = True) Begin
        Showln "Plain text body:"
        // println email.GetPlainTextBody();
    End
    Else Begin
        Get ComHasHtmlBody Of hoEmail To bTemp1
        If (bTemp1 = True) Begin
            // Convert HTML to plain-text..
            Get Create (RefClass(cComChilkatHtmlToText)) To hoH2t
            If (Not(IsComObjectCreated(hoH2t))) Begin
                Send CreateComObject of hoH2t
            End
            Showln "HTML body converted to plain-text:"
            Get ComToText Of hoH2t (ComGetHtmlBody(hoEmail)) To sTemp1
            Showln sTemp1
        End
        Else Begin
            Showln "Has no plain-text or HTML body..."
        End

    End

    Showln "---------------------------------"

    // Now get information from the message/delivery-status part (or the message/disposition-notification part)
    Showln "--- Delivery Status Information:"
    Get ComGetDeliveryStatusInfo Of hoEmail "Status" To sTemp1
    Showln "Status: " sTemp1
    Get ComGetDeliveryStatusInfo Of hoEmail "Action" To sTemp1
    Showln "Action: " sTemp1
    Get ComGetDeliveryStatusInfo Of hoEmail "Reporting-MTA" To sTemp1
    Showln "Reporting-MTA: " sTemp1

    Get ComGetDsnFinalRecipients Of hoEmail To vSa
    If (IsComObject(vSa)) Begin
        Get Create (RefClass(cComCkStringArray)) To hoSa
        Set pvComObject Of hoSa To vSa
    End
    Get ComCount Of hoSa To iNumFinalRecipients
    Move 0 To i
    While (i < iNumFinalRecipients)
        Get ComGetString Of hoSa i To sTemp1
        Showln "final recipient: " sTemp1
        Move (i + 1) To i
    Loop

    Send Destroy of hoSa

    Showln "---------------------------------"

    // If the multipart/report contains a text/rfc822-headers, it can be retrieved like this:
    Get ComGetNthTextPartOfType Of hoEmail 0 "text/rfc822-headers" False False To sHeadersText
    Get ComLastMethodSuccess Of hoEmail To bTemp1
    If (bTemp1 = True) Begin
        Showln "The text/rfc822-headers part exists.."
        Showln ""
        Showln sHeadersText

        // If you wish to process the headers, you can load them into a MIME object and use the Chilkat MIME functionality to examine the headers.
        Get Create (RefClass(cComChilkatMime)) To hoMime
        If (Not(IsComObjectCreated(hoMime))) Begin
            Send CreateComObject of hoMime
        End
        Get ComLoadMime Of hoMime sHeadersText To iSuccess

        // Do whatever you want.. 
        // For example, look at the "To" header.
        Showln "MIME To header:"
        Get ComGetHeaderField Of hoMime "To" To sTemp1
        Showln sTemp1
    End

    Showln "---------------------------------"

    // Finally, if the original email was attached, you can load it into another Chilkat Email object instance and
    // do what you want with it..

    Get ComNumAttachedMessages Of hoEmail To iTemp1
    If (iTemp1 > 0) Begin
        // Get the 1st attachment message (assume we don't have more than one attached message)
        Get ComGetAttachedMessage Of hoEmail 0 To vOrigEmail
        If (IsComObject(vOrigEmail)) Begin
            Get Create (RefClass(cComChilkatEmail)) To hoOrigEmail
            Set pvComObject Of hoOrigEmail To vOrigEmail
        End
        Get ComLastMethodSuccess Of hoEmail To bTemp1
        If (bTemp1 = True) Begin
            Get ComSubject Of hoOrigEmail To sTemp1
            Showln "Attached message subject: " sTemp1
            // Do whatever else you want..
            Send Destroy of hoOrigEmail
        End

    End



End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.