Sample code for 30+ languages & platforms
DataFlex

Get Accepted and Rejected SMTP Recipients

See more SMTP Examples

Demonstrates the Chilkat MailMan.SmtpRecipientsLog method, which returns the list of recipient email addresses accepted or rejected by the SMTP server during the most recent email-send operation. Pass true to get the rejected list or false to get the accepted list. This example sends a message to two recipients and reports both lists.

Background: A send to multiple recipients is not necessarily all-or-nothing: the server judges each RCPT TO separately, so a message can be delivered to some addresses while others are refused. Inspecting these lists after a send tells you exactly who was accepted and who was not — essential for reporting partial failures accurately or flagging bad addresses in a mailing list, rather than assuming a successful send reached everyone.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMailman
    Variant vEmail
    Handle hoEmail
    Variant vAccepted
    Handle hoAccepted
    Variant vRejected
    Handle hoRejected
    Integer n
    Integer i
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the MailMan.SmtpRecipientsLog method, which returns the list of recipient
    //  email addresses accepted or rejected by the SMTP server during the most recent email-send
    //  operation.  Pass True for the rejected list, or False for the accepted list.

    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End

    //  Configure the SMTP server connection.
    Set ComSmtpHost Of hoMailman To "smtp.example.com"
    Set ComSmtpPort Of hoMailman To 465
    Set ComSmtpSsl Of hoMailman To True
    Set ComSmtpUsername Of hoMailman To "user@example.com"
    Set ComSmtpPassword Of hoMailman To "myPassword"

    //  Send an email to two recipients.
    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Test email"
    Set ComFrom Of hoEmail To "alice@example.com"
    Get ComAddTo Of hoEmail "Bob" "bob@example.com" To iSuccess
    Get ComAddTo Of hoEmail "Carol" "carol@example.com" To iSuccess
    Set ComBody Of hoEmail To "Hello!"
    Get pvComObject of hoEmail to vEmail
    Get ComSendEmail Of hoMailman vEmail To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Get the recipients the server accepted (rejected = False).
    Get Create (RefClass(cComChilkatStringTable)) To hoAccepted
    If (Not(IsComObjectCreated(hoAccepted))) Begin
        Send CreateComObject of hoAccepted
    End
    Get pvComObject of hoAccepted to vAccepted
    Send ComSmtpRecipientsLog To hoMailman False vAccepted
    Get ComCount Of hoAccepted To iTemp1
    Showln "Accepted recipients: " iTemp1

    //  Get the recipients the server rejected (rejected = True).
    Get Create (RefClass(cComChilkatStringTable)) To hoRejected
    If (Not(IsComObjectCreated(hoRejected))) Begin
        Send CreateComObject of hoRejected
    End
    Get pvComObject of hoRejected to vRejected
    Send ComSmtpRecipientsLog To hoMailman True vRejected
    Get ComCount Of hoRejected To n
    Showln "Rejected recipients: " n

    For i From 0 To (n - 1)
        Get ComStringAt Of hoRejected i To sTemp1
        Showln sTemp1
    Loop

    //  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    //  connects and authenticates -- using the property settings above -- whenever a server
    //  operation requires it.  Calling the explicit connect/authenticate methods can still be
    //  helpful to determine whether a failure occurs while connecting or while authenticating.


End_Procedure