Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Integer iNumEmails
    Variant vEmail
    Handle hoEmail
    Variant vEForward
    Handle hoEForward
    Handle hoMailman
    String sTemp1

    Move False To iSuccess

    // 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.
    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    // Connect to an IMAP server.
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Login
    Get ComLogin Of hoImap "myLogin" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Select an IMAP mailbox
    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumMessages Of hoImap To iNumEmails

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

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get pvComObject of hoEmail to vEmail
    Get ComFetchEmail Of hoImap False iNumEmails False vEmail To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Disconnect from the IMAP server.
    Get ComDisconnect Of hoImap To iSuccess

    Get ComSubject Of hoEmail To sTemp1
    Showln sTemp1

    // Create a new email.  The email we just read will be attached to this email.
    Get Create (RefClass(cComChilkatEmail)) To hoEForward
    If (Not(IsComObjectCreated(hoEForward))) Begin
        Send CreateComObject of hoEForward
    End

    Get ComAddTo Of hoEForward "Joe" "joe@example.com" To iSuccess

    Set ComFromAddress Of hoEForward To "matt@somewhere.com"
    Set ComFromName Of hoEForward To "Matt"
    Set ComSubject Of hoEForward To "This is an email with another email attached."

    Send ComSetHtmlBody To hoEForward "<p>Hello, this is an email I'm forwarding to you.  See the attached email.</p>"

    // Attach the email.
    Get pvComObject of hoEmail to vEmail
    Get ComAttachEmail Of hoEForward vEmail To iSuccess

    // We could save the .eml, then double-click on it to view in our mail program, such as Outlook or Thunderbird..
    Get ComSaveEml Of hoEForward "qa_output/forward.eml" To iSuccess

    // We could send (forward) the email..
    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End

    Set ComSmtpHost Of hoMailman To "smtp.example.com"
    Set ComSmtpUsername Of hoMailman To "myLogin"
    Set ComSmtpPassword Of hoMailman To "myPassword"
    Set ComSmtpPort Of hoMailman To 587
    Set ComStartTLS Of hoMailman To True

    Get pvComObject of hoEForward to vEForward
    Get ComSendEmail Of hoMailman vEForward To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComCloseSmtpConnection Of hoMailman To iSuccess
    If (iSuccess <> True) Begin
        Showln "Connection to SMTP server not closed cleanly."
    End

    Showln "Mail Sent!"


End_Procedure