正在使用 Gmail 检索邮件正文 API

Retrieving message body with Gmail API

我正在尝试使用 Gmail API 提取电子邮件正文,但我无法真正检索到电子邮件正文,因为我得到的只有以下内容:

<div dir="lt
<!DOCTYPE ht

我写了以下内容来收集电子邮件的正文:

messages, err := srv.Users.Messages.List(user).Do()

for _, l := range messages.Messages {

    m, err := srv.Users.Messages.Get(user, l.Id).Do()

    if err != nil {
        log.Fatalf("Unable to retrieve labels: %v", err)
    }
    for _, part := range m.Payload.Parts {
        if part.MimeType == "text/html" {
            data, _ := base64.StdEncoding.DecodeString(part.Body.Data)
            html := string(data)
            fmt.Println(html)
        }
    }
}

关于如何获取电子邮件的实际正文,我有什么想法吗?

您应该检查为 DecodeString 返回的错误,否则您应该尝试使用 base64.URLEncoding 而不是 base64.StdEncoding 来解码数据。

来自 MessagePartBody 上的文档:

    // Data: The body data of a MIME message part as a base64url encoded
    // string. May be empty for MIME container types that have no message
    // body or when the body data is sent as a separate attachment. An
    // attachment ID is present if the body data is contained in a separate
    // attachment.
    Data string `json:"data,omitempty"`