PureBasic
PureBasic
Send Pre-Built MIME via SMTP
See more SMTP Examples
Demonstrates the Chilkat MailMan.SendMime method, which sends an email from caller-supplied MIME text. The arguments are the from address, the recipient list (the SMTP envelope recipients), and the MIME source. This example builds an email, gets its MIME, and sends that MIME.
Important: When sending caller-supplied MIME, ensure the Message-ID header is unique for each message. Sending MIME whose Message-ID was previously used can cause the message to be silently discarded as a duplicate by mail servers — see chilkatsoft.com/email_duplicate_message_id.asp.
Background: Sometimes you already have a complete message as raw MIME — loaded from a
.eml file, generated elsewhere, or archived earlier — and just need to deliver it as-is. SendMime hands that MIME straight to the SMTP server. Note the distinction between the envelope recipients (the second argument, who the server actually delivers to) and the To/Cc headers inside the MIME (what the recipient sees) — they can legitimately differ, which is how Bcc works.Chilkat PureBasic Downloads
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the MailMan.SendMime method, which sends an email from caller-supplied MIME
; text. The arguments are the from address, the recipient list (the SMTP envelope
; recipients), and the MIME source.
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")
; Obtain the MIME to send. Here we build an Email and get its MIME, but the MIME could
; come from any source.
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkSubject(email, "Test email")
CkEmail::setCkFrom(email, "alice@example.com")
CkEmail::ckAddTo(email,"Bob","bob@example.com")
CkEmail::setCkBody(email, "Hello, this message was sent as MIME.")
mimeText.s = CkEmail::ckGetMime(email)
; IMPORTANT: When sending caller-supplied MIME, make sure the Message-ID header is unique
; for each message. Sending MIME that contains a Message-ID that was previously sent can
; cause the message to be silently discarded as a duplicate by mail servers. For details,
; see: https://www.chilkatsoft.com/email_duplicate_message_id.asp
; Send the MIME to the envelope recipient(s).
success = CkMailMan::ckSendMime(mailman,"alice@example.com","bob@example.com",mimeText)
If success = 0
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
Debug "MIME 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