Sample code for 30+ languages & platforms
PureBasic

Verify Email Recipients Without Sending

See more SMTP Examples

Demonstrates the Chilkat MailMan.VerifyRecips method, which begins the SMTP send process for an email, sends the recipient addresses to the SMTP server, and then aborts before sending the message content. Recipient addresses rejected by the server are returned in the StringArray. This example checks two recipients and lists any that were rejected.

Background: During an SMTP transaction each recipient is offered with a RCPT TO command, and the server accepts or rejects it before any message content is transferred. VerifyRecips exploits that: it runs the transaction just far enough to collect those verdicts, then aborts — letting you validate an address list without actually delivering mail. Note that many servers accept everything at this stage and bounce later, so a clean result is not a guarantee of deliverability.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringArray.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the MailMan.VerifyRecips method, which begins the SMTP send process for an
    ;  email, sends the recipient addresses to the SMTP server, and then aborts before sending
    ;  the message content.  Recipient addresses rejected by the server are returned in the
    ;  StringArray.

    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Configure the SMTP server connection.
    CkMailMan::setCkSmtpHost(mailman, "smtp.example.com")
    CkMailMan::setCkSmtpPort(mailman, 465)
    CkMailMan::setCkSmtpSsl(mailman, 1)
    CkMailMan::setCkSmtpUsername(mailman, "user@example.com")
    CkMailMan::setCkSmtpPassword(mailman, "myPassword")

    ;  Build an email with the recipients to verify.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Recipient check")
    CkEmail::setCkFrom(email, "alice@example.com")
    CkEmail::ckAddTo(email,"Bob","bob@example.com")
    CkEmail::ckAddTo(email,"Nobody","nobody@example.com")

    ;  Ask the server which recipients it will accept.  Rejected addresses are returned.
    badAddrs.i = CkStringArray::ckCreate()
    If badAddrs.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMailMan::ckVerifyRecips(mailman,email,badAddrs)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        CkEmail::ckDispose(email)
        CkStringArray::ckDispose(badAddrs)
        ProcedureReturn
    EndIf

    n.i = CkStringArray::ckCount(badAddrs)
    Debug "Rejected recipients: " + Str(n)
    i.i
    For i = 0 To n - 1
        Debug CkStringArray::ckGetString(badAddrs,i)
    Next

    ;  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.


    CkMailMan::ckDispose(mailman)
    CkEmail::ckDispose(email)
    CkStringArray::ckDispose(badAddrs)


    ProcedureReturn
EndProcedure