Sample code for 30+ languages & platforms
Go

Append MIME from a StringBuilder with Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlagsSb method, which uploads MIME contained in a StringBuilder and sets its initial system flags. The first argument is the mailbox, the second is the StringBuilder, and the third through sixth set \Seen, \Flagged, \Answered, and \Draft. This example uploads to Drafts with the draft flag set.

Background: This is the StringBuilder form of AppendMimeWithFlags. Passing the MIME as a StringBuilder avoids copying a large message into an intermediate string and is convenient when you already built or loaded the message into a StringBuilder (for example with LoadFile).

Chilkat Go Downloads

Go
    success := false

    //  Demonstrates the Imap.AppendMimeWithFlagsSb method, which uploads MIME contained in a
    //  StringBuilder and sets its initial system flags.  The 1st argument is the mailbox, the 2nd
    //  is the StringBuilder, and the 3rd through 6th set \Seen, \Flagged, \Answered, and \Draft.

    imap := chilkat.NewImap()

    imap.SetSsl(true)
    imap.SetPort(993)

    success = imap.Connect("imap.example.com")
    if success == false {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        return
    }

    success = imap.Login("user@example.com","myPassword")
    if success == false {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        return
    }

    //  Build the email to be uploaded.
    email := chilkat.NewEmail()
    email.SetSubject("Meeting agenda")
    email.SetFrom("Alice <alice@example.com>")
    success = email.AddTo("Bob","bob@example.com")
    email.SetBody("Let's meet at 10am to review the agenda.")

    mimeText := email.GetMime()
    if email.LastMethodSuccess() == false {
        fmt.Println(email.LastErrorText())
        imap.DisposeImap()
        email.DisposeEmail()
        return
    }

    //  Load the MIME into a StringBuilder.  (A StringBuilder can also be filled via LoadFile.)
    sbMime := chilkat.NewStringBuilder()
    sbMime.Append(*mimeText)

    //  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
    seen := false
    flagged := false
    answered := false
    draft := true

    //  Upload to "Drafts" with the \Draft flag set.
    success = imap.AppendMimeWithFlagsSb("Drafts",sbMime,seen,flagged,answered,draft)
    if success == false {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        email.DisposeEmail()
        sbMime.DisposeStringBuilder()
        return
    }

    fmt.Println("Appended UID: ", imap.AppendUid())

    success = imap.Disconnect()
    if success == false {
        fmt.Println(imap.LastErrorText())
        imap.DisposeImap()
        email.DisposeEmail()
        sbMime.DisposeStringBuilder()
        return
    }


    imap.DisposeImap()
    email.DisposeEmail()
    sbMime.DisposeStringBuilder()