Sample code for 30+ languages & platforms
PureBasic

Send an Email via SMTP

See more SMTP Examples

Demonstrates the Chilkat MailMan.SendEmail method, which sends a single Email object through the configured SMTP server. This example configures the SMTP connection, builds a message, and sends it.

Background: SMTP (Simple Mail Transfer Protocol) is the protocol for sending mail. The typical flow is: connect to the server, authenticate, then hand over the message with MAIL FROM, RCPT TO, and DATA. SendEmail wraps all of that: you build an Email with subject, recipients, and body, and Chilkat renders it to MIME and delivers it. Modern submission commonly uses implicit TLS on port 465 (SmtpSsl = true).

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the MailMan.SendEmail method, which sends a single Email object through the
    ;  configured SMTP server.

    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 the email to send.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Test email from Chilkat")
    CkEmail::setCkFrom(email, "alice@example.com")
    CkEmail::ckAddTo(email,"Bob","bob@example.com")
    CkEmail::setCkBody(email, "Hello, this is a test message.")

    ;  Send the email.

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

    Debug "Email sent."

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


    ProcedureReturn
EndProcedure