You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
4.9 KiB
218 lines
4.9 KiB
5 years ago
|
package tools
|
||
5 years ago
|
|
||
|
import (
|
||
5 years ago
|
"bufio"
|
||
5 years ago
|
"flag"
|
||
|
"fmt"
|
||
5 years ago
|
"io"
|
||
|
"io/ioutil"
|
||
5 years ago
|
"log"
|
||
5 years ago
|
"os"
|
||
|
"strconv"
|
||
|
"strings"
|
||
5 years ago
|
|
||
5 years ago
|
"github.com/emersion/go-imap"
|
||
5 years ago
|
"github.com/emersion/go-imap/client"
|
||
5 years ago
|
"github.com/emersion/go-message/mail"
|
||
5 years ago
|
)
|
||
|
|
||
|
//全局变量
|
||
|
//imap服务地址,邮箱,密码
|
||
|
var (
|
||
|
server, email, password string
|
||
5 years ago
|
folders map[int]string
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
func testmain() {
|
||
5 years ago
|
|
||
5 years ago
|
//获取参数中的数据
|
||
|
flag.StringVar(&server, "server", "", "imap服务地址(包含端口)")
|
||
|
flag.StringVar(&email, "email", "", "邮箱名")
|
||
|
flag.StringVar(&password, "password", "", "密码")
|
||
|
flag.Parse()
|
||
|
if flag.NFlag() < 3 {
|
||
|
flag.PrintDefaults()
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
if server == "" || email == "" || password == "" {
|
||
|
log.Fatal("服务器地址,用户名,密码,参数必填")
|
||
|
}
|
||
5 years ago
|
log.Println("正在连接服务器...")
|
||
5 years ago
|
//支持加密和非加密端口
|
||
5 years ago
|
if !strings.Contains(server, ":") {
|
||
|
log.Fatal("服务器地址端口号错误:", server)
|
||
|
}
|
||
5 years ago
|
serverSlice := strings.Split(server, ":")
|
||
|
uri := serverSlice[0]
|
||
|
port, _ := strconv.Atoi(serverSlice[1])
|
||
|
if port != 993 && port != 143 {
|
||
5 years ago
|
log.Fatal("服务器地址端口号错误:", port)
|
||
5 years ago
|
}
|
||
5 years ago
|
// 连接到服务器
|
||
5 years ago
|
var c *client.Client
|
||
|
var err error
|
||
|
if port == 993 {
|
||
|
c, err = client.DialTLS(fmt.Sprintf("%s:%d", uri, port), nil)
|
||
|
} else {
|
||
|
c, err = client.Dial(fmt.Sprintf("%s:%d", uri, port))
|
||
|
}
|
||
5 years ago
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
log.Println("连接成功")
|
||
|
|
||
|
// 不要忘了退出
|
||
|
defer c.Logout()
|
||
|
|
||
|
// 登陆
|
||
5 years ago
|
if err := c.Login(email, password); err != nil {
|
||
5 years ago
|
log.Fatal(err)
|
||
|
}
|
||
|
log.Println("成功登陆")
|
||
|
|
||
|
// 列邮箱
|
||
|
mailboxes := make(chan *imap.MailboxInfo, 10)
|
||
|
done := make(chan error, 1)
|
||
5 years ago
|
go func() {
|
||
5 years ago
|
done <- c.List("", "*", mailboxes)
|
||
|
}()
|
||
5 years ago
|
// 存储邮件夹
|
||
|
folders = make(map[int]string)
|
||
|
i := 1
|
||
5 years ago
|
for m := range mailboxes {
|
||
5 years ago
|
log.Println("* ", i, m.Name)
|
||
|
folders[i] = m.Name
|
||
|
i++
|
||
5 years ago
|
}
|
||
5 years ago
|
log.Println("输入邮件夹序号:")
|
||
|
inLine := readLineFromInput()
|
||
|
folderNum, _ := strconv.Atoi(inLine)
|
||
|
currentFolder := folders[folderNum]
|
||
5 years ago
|
|
||
|
if err := <-done; err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
5 years ago
|
// Select 邮件夹
|
||
|
mbox, err := c.Select(currentFolder, false)
|
||
5 years ago
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
5 years ago
|
log.Printf("%s的邮件个数:%d \r\n", currentFolder, mbox.Messages)
|
||
|
|
||
|
// 获取最新的信
|
||
5 years ago
|
log.Println("读取最新的几封信(all全部):")
|
||
5 years ago
|
inLine = readLineFromInput()
|
||
5 years ago
|
var maxNum uint32
|
||
5 years ago
|
if inLine == "all" {
|
||
|
maxNum = mbox.Messages
|
||
|
} else {
|
||
5 years ago
|
tempNum, _ := strconv.Atoi(inLine)
|
||
5 years ago
|
maxNum = uint32(tempNum)
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
from := uint32(1)
|
||
|
to := mbox.Messages
|
||
5 years ago
|
if mbox.Messages >= maxNum {
|
||
5 years ago
|
// 我们在这使用无符号整型, 这是再获取from的id
|
||
5 years ago
|
from = mbox.Messages - maxNum + 1
|
||
5 years ago
|
} else {
|
||
|
log.Fatal("超出了邮件封数!")
|
||
5 years ago
|
}
|
||
|
seqset := new(imap.SeqSet)
|
||
|
seqset.AddRange(from, to)
|
||
|
|
||
|
messages := make(chan *imap.Message, 10)
|
||
|
done = make(chan error, 1)
|
||
|
go func() {
|
||
|
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
|
||
|
}()
|
||
|
|
||
5 years ago
|
log.Printf("最新的 %d 封信:", maxNum)
|
||
5 years ago
|
for msg := range messages {
|
||
5 years ago
|
log.Printf("* %d:%s\n", to, msg.Envelope.Subject)
|
||
5 years ago
|
to--
|
||
5 years ago
|
}
|
||
|
|
||
|
if err := <-done; err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
5 years ago
|
for {
|
||
5 years ago
|
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 {
|
||
5 years ago
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
5 years ago
|
//打印信息
|
||
|
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)
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
log.Printf("读取第 %d 封信, 结束!\n", mesIndex)
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
//从输入中读取一行
|
||
|
func readLineFromInput() string {
|
||
|
str := ""
|
||
|
scanner := bufio.NewScanner(os.Stdin)
|
||
|
for scanner.Scan() {
|
||
|
str = scanner.Text() // Println will add back the final '\n'
|
||
|
break
|
||
|
}
|
||
|
return str
|
||
|
}
|