-
Notifications
You must be signed in to change notification settings - Fork 3
/
imap.go
101 lines (89 loc) · 2.07 KB
/
imap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package mailKit
import (
"crypto/tls"
"github.com/emersion/go-imap"
id "github.com/emersion/go-imap-id"
"github.com/emersion/go-imap/client"
"github.com/richelieu-yang/chimera/v2/src/core/errorKit"
"sync"
)
type (
ImapConfig struct {
Address string
TlsConfig *tls.Config
UserName string
Password string
}
)
var imapLock = &sync.RWMutex{}
var imapConfig *ImapConfig
func InitializeImap(config *ImapConfig) error {
imapLock.Lock()
defer imapLock.Unlock()
c, err := newImapClient(config)
if err != nil {
return err
}
defer func() {
_ = c.Close()
}()
imapConfig = config
return nil
}
// newImapClient 连接服务器 && 登录
/*
@param addr e.g. "imap.163.com:993"
@param username 邮箱,e.g. "miro42@163.com"
@param password 邮箱的授权密码(并非登录密码!!!),e.g. "ZRZUBJXZOFIOBNXM"
*/
func newImapClient(config *ImapConfig) (c *client.Client, err error) {
defer func() {
// 如果创建客户端失败,为防万一,额外调用下Logout()
if err != nil && c != nil {
_ = c.Logout()
}
}()
if config == nil {
err = errorKit.New("config == nil")
return
}
// (1) 连接服务器
if c, err = client.DialTLS(config.Address, config.TlsConfig); err != nil {
return
}
// (2) 登录
err = c.Login(config.UserName, config.Password)
if err != nil {
return
}
// (3) 添加ID信息(以防收取163邮箱报错(error): SELECT Unsafe Login. Please contact kefu@188.com for help)
idClient := id.NewClient(c)
_, err = idClient.ID(id.ID{
id.FieldName: "IMAPClient",
id.FieldVersion: "3.1.0",
})
return
}
func GetMailboxInfoSlice() ([]*imap.MailboxInfo, error) {
c, err := newImapClient(imapConfig)
if err != nil {
return nil, err
}
defer func() {
_ = c.Close()
}()
ch := make(chan *imap.MailboxInfo, 10)
done := make(chan error, 1)
go func() {
done <- c.List("", "*", ch)
}()
// 流程: 先处理ch信道,再处理done信道(不能反过来!)
s := make([]*imap.MailboxInfo, 0, len(ch))
for m := range ch {
s = append(s, m)
}
if err := <-done; err != nil {
return nil, err
}
return s, nil
}