Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

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

    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

    Get Create (RefClass(cComChilkatEmail)) To hoEForward
    If (Not(IsComObjectCreated(hoEForward))) Begin
        Send CreateComObject of hoEForward
    End
    Get pvComObject of hoEForward to vEForward
    Get ComToForward Of hoEmail vEForward To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The eForward email has no To or CC recipients yet.
    // Add one or more..
    Get ComAddTo Of hoEForward "Joe" "joe@example.com" To iSuccess

    // We also need to specify the From name/address.
    Set ComFromAddress Of hoEForward To "matt@someMailServer.com"
    Set ComFromName Of hoEForward To "Matt"

    // If we wish to add text at the start of the email body:
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHtmlBody
    If (Not(IsComObjectCreated(hoSbHtmlBody))) Begin
        Send CreateComObject of hoSbHtmlBody
    End
    Get ComHasHtmlBody Of hoEForward To bTemp1
    If (bTemp1 = True) Begin
        Get ComGetHtmlBody Of hoEForward To sTemp1
        Get ComAppend Of hoSbHtmlBody sTemp1 To iSuccess
        Get ComPrepend Of hoSbHtmlBody "<p>Hello, this is an email I'm forwarding to you...</p>" To iSuccess
        Get ComGetAsString Of hoSbHtmlBody To sTemp1
        Send ComSetHtmlBody To hoEForward sTemp1
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPtBody
    If (Not(IsComObjectCreated(hoSbPtBody))) Begin
        Send CreateComObject of hoSbPtBody
    End
    Get ComHasPlainTextBody Of hoEForward To bTemp1
    If (bTemp1 = True) Begin
        Get ComGetPlainTextBody Of hoEForward To sTemp1
        Get ComAppend Of hoSbPtBody sTemp1 To iSuccess
        Get ComPrepend Of hoSbPtBody "Hello, this is an email I'm forwarding to you..." + (character(13)) + (character(10)) + (character(13)) + (character(10)) To iSuccess
        Get ComGetAsString Of hoSbPtBody To sTemp1
        Send ComSetTextBody To hoEForward sTemp1 "text/plain"
    End

    // 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