|
|
|
@ -137,69 +137,72 @@ func main() {
|
|
|
|
|
if err := <-done; err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
log.Println("读取第几封信信体:")
|
|
|
|
|
inLine = readLineFromInput()
|
|
|
|
|
mesIndex, _ := strconv.Atoi(inLine)
|
|
|
|
|
mesSeqSet := new(imap.SeqSet)
|
|
|
|
|
mesSeqSet.AddNum(uint32(mesIndex))
|
|
|
|
|
//获取整封信体
|
|
|
|
|
var section imap.BodySectionName
|
|
|
|
|
items := []imap.FetchItem{section.FetchItem()}
|
|
|
|
|
|
|
|
|
|
messages = make(chan *imap.Message, 1)
|
|
|
|
|
go func() {
|
|
|
|
|
if err := c.Fetch(mesSeqSet, items, messages); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
msg := <-messages
|
|
|
|
|
if msg == nil {
|
|
|
|
|
log.Fatal("服务器没有返回信息")
|
|
|
|
|
}
|
|
|
|
|
r := msg.GetBody(§ion)
|
|
|
|
|
if r == nil {
|
|
|
|
|
log.Fatal("服务器没有返回 message body")
|
|
|
|
|
}
|
|
|
|
|
mr, err := mail.CreateReader(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//打印信息
|
|
|
|
|
header := mr.Header
|
|
|
|
|
if date, err := header.Date(); err == nil {
|
|
|
|
|
log.Println("Date:", date)
|
|
|
|
|
}
|
|
|
|
|
if from, err := header.AddressList("From"); err == nil {
|
|
|
|
|
log.Println("From:", from)
|
|
|
|
|
}
|
|
|
|
|
if to, err := header.AddressList("To"); err == nil {
|
|
|
|
|
log.Println("To:", to)
|
|
|
|
|
}
|
|
|
|
|
if subject, err := header.Subject(); err == nil {
|
|
|
|
|
log.Println("Subject:", subject)
|
|
|
|
|
}
|
|
|
|
|
// Process each message's part
|
|
|
|
|
for {
|
|
|
|
|
p, err := mr.NextPart()
|
|
|
|
|
if err == io.EOF {
|
|
|
|
|
break
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
log.Println("读取第几封信信体:")
|
|
|
|
|
|
|
|
|
|
inLine = readLineFromInput()
|
|
|
|
|
mesIndex, _ := strconv.Atoi(inLine)
|
|
|
|
|
mesSeqSet := new(imap.SeqSet)
|
|
|
|
|
mesSeqSet.AddNum(uint32(mesIndex))
|
|
|
|
|
//获取整封信体
|
|
|
|
|
var section imap.BodySectionName
|
|
|
|
|
items := []imap.FetchItem{section.FetchItem()}
|
|
|
|
|
|
|
|
|
|
bodyMsg := make(chan *imap.Message, 1)
|
|
|
|
|
go func() {
|
|
|
|
|
if err := c.Fetch(mesSeqSet, items, bodyMsg); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
msg := <-bodyMsg
|
|
|
|
|
if msg == nil {
|
|
|
|
|
log.Fatal("服务器没有返回信息")
|
|
|
|
|
}
|
|
|
|
|
r := msg.GetBody(§ion)
|
|
|
|
|
if r == nil {
|
|
|
|
|
log.Fatal("服务器没有返回 message body")
|
|
|
|
|
}
|
|
|
|
|
mr, err := mail.CreateReader(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch h := p.Header.(type) {
|
|
|
|
|
case *mail.InlineHeader:
|
|
|
|
|
// This is the message's text (can be plain-text or HTML)
|
|
|
|
|
b, _ := ioutil.ReadAll(p.Body)
|
|
|
|
|
log.Printf("Got text: %s\n", string(b))
|
|
|
|
|
case *mail.AttachmentHeader:
|
|
|
|
|
// This is an attachment
|
|
|
|
|
filename, _ := h.Filename()
|
|
|
|
|
log.Printf("Got attachment: %s\n", filename)
|
|
|
|
|
//打印信息
|
|
|
|
|
header := mr.Header
|
|
|
|
|
if date, err := header.Date(); err == nil {
|
|
|
|
|
log.Println("Date:", date)
|
|
|
|
|
}
|
|
|
|
|
if from, err := header.AddressList("From"); err == nil {
|
|
|
|
|
log.Println("From:", from)
|
|
|
|
|
}
|
|
|
|
|
if to, err := header.AddressList("To"); err == nil {
|
|
|
|
|
log.Println("To:", to)
|
|
|
|
|
}
|
|
|
|
|
if subject, err := header.Subject(); err == nil {
|
|
|
|
|
log.Println("Subject:", subject)
|
|
|
|
|
}
|
|
|
|
|
// Process each message's part
|
|
|
|
|
for {
|
|
|
|
|
p, err := mr.NextPart()
|
|
|
|
|
if err == io.EOF {
|
|
|
|
|
break
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch h := p.Header.(type) {
|
|
|
|
|
case *mail.InlineHeader:
|
|
|
|
|
// This is the message's text (can be plain-text or HTML)
|
|
|
|
|
b, _ := ioutil.ReadAll(p.Body)
|
|
|
|
|
log.Printf("Got text: %s\n", string(b))
|
|
|
|
|
case *mail.AttachmentHeader:
|
|
|
|
|
// This is an attachment
|
|
|
|
|
filename, _ := h.Filename()
|
|
|
|
|
log.Printf("Got attachment: %s\n", filename)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.Printf("读取第 %d 封信, 结束!\n", mesIndex)
|
|
|
|
|
}
|
|
|
|
|
log.Println("结束!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//从输入中读取一行
|
|
|
|
|