PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_NumEmails
oleobject loo_Email
oleobject loo_EForward
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
// Create a new email. The email we just read will be attached to this email.
loo_EForward = create oleobject
li_rc = loo_EForward.ConnectToNewObject("Chilkat.Email")
loo_EForward.AddTo("Joe","joe@example.com")
loo_EForward.FromAddress = "matt@somewhere.com"
loo_EForward.FromName = "Matt"
loo_EForward.Subject = "This is an email with another email attached."
loo_EForward.SetHtmlBody("<p>Hello, this is an email I'm forwarding to you. See the attached email.</p>")
// Attach the email.
loo_EForward.AttachEmail(loo_Email)
// 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_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_Mailman