3 回答
TA贡献1784条经验 获得超7个赞
我在github.com/jhillyerd/enmime包中很幸运地做到了这一点。给定一个io.Reader r:
// Parse message body
env, _ := enmime.ReadEnvelope(r)
// Headers can be retrieved via Envelope.GetHeader(name).
fmt.Printf("From: %v\n", env.GetHeader("From"))
// Address-type headers can be parsed into a list of decoded mail.Address structs.
alist, _ := env.AddressList("To")
for _, addr := range alist {
fmt.Printf("To: %s <%s>\n", addr.Name, addr.Address)
}
fmt.Printf("Subject: %v\n", env.GetHeader("Subject"))
// The plain text body is available as mime.Text.
fmt.Printf("Text Body: %v chars\n", len(env.Text))
// The HTML body is stored in mime.HTML.
fmt.Printf("HTML Body: %v chars\n", len(env.HTML))
// mime.Inlines is a slice of inlined attacments.
fmt.Printf("Inlines: %v\n", len(env.Inlines))
// mime.Attachments contains the non-inline attachments.
fmt.Printf("Attachments: %v\n", len(env.Attachments))
TA贡献1776条经验 获得超12个赞
这是我的示例:缺少的部分是提取附件。如果你想通了那部分,请告诉我......几个星期以来我一直在挠头提取附件......
import (
"fmt"
"io/ioutil"
"net/mail"
)
func extractEmail(mail *mail.Message){
header := mail.Header
fmt.Println(header.Get("Date"))
fmt.Println(header.Get("From"))
fmt.Println(header.Get("To"))
fmt.Println(header.Get("cc"))
fmt.Println(header.Get("bcc"))
fmt.Println(header.Get("Subject"))
body, err := ioutil.ReadAll(mail.Body)
if err != nil {
checkErr(err, "Reading Body")
}
fmt.Println(body)
}
- 3 回答
- 0 关注
- 244 浏览
添加回答
举报