简体   繁体   中英

How do I create a draft in Gmail using IMAP using GO

I would like to use GO to create a new message ( specifically a draft ) that is stored in my inbox with everything ready to hit send at a later date. With SMTP I think it won't work. With IMAP maybe I can. How do I go about this?

package main

import (
    "bytes"
    "log"
    "os"
    "time"

    "github.com/emersion/go-imap"
    "github.com/emersion/go-imap/client"
)

func main() {
    log.Println("Connecting to server...")

    // Connect to server
    c, err := client.DialTLS(os.Getenv("IMAP_SERVER"), nil)
    if err != nil {
        log.Fatal(err)
    }
    log.Println("Connected")

    // Don't forget to logout
    defer c.Logout()

    // Login
    if err := c.Login(os.Getenv("IMAP_USER"), os.Getenv("IMAP_PASSWORD")); err != nil {
        log.Fatal(err)
    }
    log.Println("Logged in")

    // Write the message to a buffer
    var b bytes.Buffer
    b.WriteString("From: <...@gmail.com>\r\n")
    b.WriteString("To: <...@gmail.com>\r\n")
    b.WriteString("Subject: Append test\r\n")
    b.WriteString("\r\n")
    // Message body
    b.WriteString("Append test using Gmail IMAP and Draft folder")

    // Append it to Drafts
    if err := c.Append("[Gmail]/Drafts", nil, time.Now(), &b); err != nil {
        log.Fatal(err)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM