Xbase++ Requires Chilkat v11.0.0+
Xbase++
Forward by Attaching the Existing Email to a New Email
See more Email Object Examples
Demonstrates how to forward an email by attaching the email to a new email.This example reads an email from an IMAP server, attaches the email to a new email, and sends the new email.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL nNumEmails
LOCAL oEmail
LOCAL oEForward
LOCAL oMailman
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Read the 1st (most recent) email in an Inbox.
oImap := CreateObject("Chilkat.Imap")
// Connect to an IMAP server.
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
// Login
nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
// Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
nNumEmails := oImap:NumMessages
// Fetch the email at the last sequence number.
// (We are assuming the Inbox has at least 1 email)
oEmail := CreateObject("Chilkat.Email")
nSuccess := oImap:FetchEmail(0, nNumEmails, 0, oEmail)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oEmail:destroy()
RETURN
ENDIF
// Disconnect from the IMAP server.
oImap:Disconnect()
? oEmail:Subject
// Create a new email. The email we just read will be attached to this email.
oEForward := CreateObject("Chilkat.Email")
oEForward:AddTo("Joe", "joe@example.com")
oEForward:FromAddress := "matt@somewhere.com"
oEForward:FromName := "Matt"
oEForward:Subject := "This is an email with another email attached."
oEForward:SetHtmlBody("<p>Hello, this is an email I'm forwarding to you. See the attached email.</p>")
// Attach the email.
oEForward:AttachEmail(oEmail)
// We could save the .eml, then double-click on it to view in our mail program, such as Outlook or Thunderbird..
oEForward:SaveEml("qa_output/forward.eml")
// We could send (forward) the email..
oMailman := CreateObject("Chilkat.MailMan")
oMailman:SmtpHost := "smtp.example.com"
oMailman:SmtpUsername := "myLogin"
oMailman:SmtpPassword := "myPassword"
oMailman:SmtpPort := 587
oMailman:StartTLS := 1
nSuccess := oMailman:SendEmail(oEForward)
IF (nSuccess == 0)
? oMailman:LastErrorText
oImap:destroy()
oEmail:destroy()
oEForward:destroy()
oMailman:destroy()
RETURN
ENDIF
nSuccess := oMailman:CloseSmtpConnection()
IF (nSuccess != 1)
? "Connection to SMTP server not closed cleanly."
ENDIF
? "Mail Sent!"
oImap:destroy()
oEmail:destroy()
oEForward:destroy()
oMailman:destroy()