Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Forward an Email using CreateForward

See more Email Object Examples

Reads an email from an IMAP server, creates a forward version of the email using the CreateForward method, and sends the email to another recipient.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nNumEmails
LOCAL oEmail
LOCAL oEForward
LOCAL oSbHtmlBody
LOCAL oSbPtBody
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

oEForward := CreateObject("Chilkat.Email")
nSuccess := oEmail:ToForward(oEForward)
IF (nSuccess == 0)
    ? oEmail:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    oEForward:destroy()
    RETURN
ENDIF

//  The eForward email has no To or CC recipients yet.
//  Add one or more..
oEForward:AddTo("Joe", "joe@example.com")

//  We also need to specify the From name/address.
oEForward:FromAddress := "matt@someMailServer.com"
oEForward:FromName := "Matt"

//  If we wish to add text at the start of the email body:
oSbHtmlBody := CreateObject("Chilkat.StringBuilder")
IF (oEForward:HasHtmlBody() == 1)
    oSbHtmlBody:Append(oEForward:GetHtmlBody())
    oSbHtmlBody:Prepend("<p>Hello, this is an email I'm forwarding to you...</p>")
    oEForward:SetHtmlBody(oSbHtmlBody:GetAsString())
ENDIF

oSbPtBody := CreateObject("Chilkat.StringBuilder")
IF (oEForward:HasPlainTextBody() == 1)
    oSbPtBody:Append(oEForward:GetPlainTextBody())
    oSbPtBody:Prepend("Hello, this is an email I'm forwarding to you..." + Chr(13) + Chr(10) + Chr(13) + Chr(10))
    oEForward:SetTextBody(oSbPtBody:GetAsString(), "text/plain")
ENDIF

//  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()
    oSbHtmlBody:destroy()
    oSbPtBody: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()
oSbHtmlBody:destroy()
oSbPtBody:destroy()
oMailman:destroy()