Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_NumEmails
oleobject loo_Email
oleobject loo_EForward
oleobject loo_SbHtmlBody
oleobject loo_SbPtBody
oleobject loo_Mailman

li_Success = 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.
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Connect to an IMAP server.
loo_Imap.Ssl = 1
loo_Imap.Port = 993
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Login
li_Success = loo_Imap.Login("myLogin","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Select an IMAP mailbox
li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_NumEmails = loo_Imap.NumMessages

// Fetch the email at the last sequence number.
// (We are assuming the Inbox has at least 1 email)

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Imap.FetchEmail(0,li_NumEmails,0,loo_Email)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    return
end if

// Disconnect from the IMAP server.
loo_Imap.Disconnect()

Write-Debug loo_Email.Subject

loo_EForward = create oleobject
li_rc = loo_EForward.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Email.ToForward(loo_EForward)
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    destroy loo_EForward
    return
end if

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

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

// If we wish to add text at the start of the email body:
loo_SbHtmlBody = create oleobject
li_rc = loo_SbHtmlBody.ConnectToNewObject("Chilkat.StringBuilder")

if loo_EForward.HasHtmlBody() = 1 then
    loo_SbHtmlBody.Append(loo_EForward.GetHtmlBody())
    loo_SbHtmlBody.Prepend("<p>Hello, this is an email I'm forwarding to you...</p>")
    loo_EForward.SetHtmlBody(loo_SbHtmlBody.GetAsString())
end if

loo_SbPtBody = create oleobject
li_rc = loo_SbPtBody.ConnectToNewObject("Chilkat.StringBuilder")

if loo_EForward.HasPlainTextBody() = 1 then
    loo_SbPtBody.Append(loo_EForward.GetPlainTextBody())
    loo_SbPtBody.Prepend("Hello, this is an email I'm forwarding to you...~r~n~r~n")
    loo_EForward.SetTextBody(loo_SbPtBody.GetAsString(),"text/plain")
end if

// We could save the .eml, then double-click on it to view in our mail program, such as Outlook or Thunderbird..
loo_EForward.SaveEml("qa_output/forward.eml")

// We could send (forward) the email..
loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")

loo_Mailman.SmtpHost = "smtp.example.com"
loo_Mailman.SmtpUsername = "myLogin"
loo_Mailman.SmtpPassword = "myPassword"
loo_Mailman.SmtpPort = 587
loo_Mailman.StartTLS = 1

li_Success = loo_Mailman.SendEmail(loo_EForward)
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    destroy loo_EForward
    destroy loo_SbHtmlBody
    destroy loo_SbPtBody
    destroy loo_Mailman
    return
end if

li_Success = loo_Mailman.CloseSmtpConnection()
if li_Success <> 1 then
    Write-Debug "Connection to SMTP server not closed cleanly."
end if

Write-Debug "Mail Sent!"


destroy loo_Imap
destroy loo_Email
destroy loo_EForward
destroy loo_SbHtmlBody
destroy loo_SbPtBody
destroy loo_Mailman