解决没有mailreader时崩溃问题

pull/30/head
陶士涵 4 years ago
parent af503b77b9
commit bd93b64dbb

@ -24,11 +24,9 @@
### 安装使用 ### 安装使用
1.git clone https://github.com/taoshihan1991/imaptool.git 1.在$GOPATH的src/github.com下 git clone https://github.com/taoshihan1991/imaptool.git
2.进入目录执行 go mod tidy 2.源码运行 go run server.go
3.源码运行 go run server.go 3.源码打包 go build server.go
4.源码打包 go build server.go

@ -179,15 +179,15 @@ func GetMessage(server string, email string, password string, folder string, id
if r == nil { if r == nil {
log.Fatal("Server didn't returned message body") log.Fatal("Server didn't returned message body")
} }
var mailitem = new(MailItem)
// Create a new mail reader // Create a new mail reader
mr, err := mail.CreateReader(r) mr, err := mail.CreateReader(r)
if err != nil { if err != nil {
//log.Fatal(err) return mailitem
} }
var mailitem = new(MailItem)
// Print some info about the message // Print some info about the message
header := mr.Header header := mr.Header
date, _ := header.Date() date, _ := header.Date()
@ -226,9 +226,9 @@ func GetMessage(server string, email string, password string, folder string, id
log.Println("Subject:", s) log.Println("Subject:", s)
mailitem.Subject = s mailitem.Subject = s
// Process each message's part // Process each message's part
var bodyMap=make(map[string]string) var bodyMap = make(map[string]string)
bodyMap["text/plain"]= "" bodyMap["text/plain"] = ""
bodyMap["text/html"]= "" bodyMap["text/html"] = ""
for { for {
p, err := mr.NextPart() p, err := mr.NextPart()
@ -242,11 +242,11 @@ func GetMessage(server string, email string, password string, folder string, id
// This is the message's text (can be plain-text or HTML) // This is the message's text (can be plain-text or HTML)
b, _ := ioutil.ReadAll(p.Body) b, _ := ioutil.ReadAll(p.Body)
ct:=p.Header.Get("Content-Type") ct := p.Header.Get("Content-Type")
if strings.Contains(ct,"text/plain"){ if strings.Contains(ct, "text/plain") {
bodyMap["text/plain"]+=Encoding(string(b),ct) bodyMap["text/plain"] += Encoding(string(b), ct)
}else{ } else {
bodyMap["text/html"]+=Encoding(string(b),ct) bodyMap["text/html"] += Encoding(string(b), ct)
} }
//body,_:=dec.Decode(string(b)) //body,_:=dec.Decode(string(b))
case *mail.AttachmentHeader: case *mail.AttachmentHeader:
@ -256,10 +256,10 @@ func GetMessage(server string, email string, password string, folder string, id
} }
} }
if bodyMap["text/html"]!=""{ if bodyMap["text/html"] != "" {
mailitem.Body =bodyMap["text/html"] mailitem.Body = bodyMap["text/html"]
}else{ } else {
mailitem.Body =bodyMap["text/plain"] mailitem.Body = bodyMap["text/plain"]
} }
//log.Println(mailitem.Body) //log.Println(mailitem.Body)
return mailitem return mailitem

@ -1,4 +1,4 @@
package main package tools
import ( import (
"bufio" "bufio"
@ -23,7 +23,7 @@ var (
folders map[int]string folders map[int]string
) )
func main() { func testmain() {
//获取参数中的数据 //获取参数中的数据
flag.StringVar(&server, "server", "", "imap服务地址(包含端口)") flag.StringVar(&server, "server", "", "imap服务地址(包含端口)")
Loading…
Cancel
Save