我正在尝试用Go连接IMAP服务器,我可以用openssl
连接并发出命令:
openssl s_client -crlf -connect mail.privateemail.com:993
CONNECTED(00000003)
[...]
test LOGIN myuser@email.com myp4ssw0rd
test OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SNIPPET=FUZZY PREVIEW=FUZZY PREVIEW STATUS=SIZE SAVEDATE SEARCH=X-MIMEPART XDOVECOT LITERAL+ NOTIFY METADATA SPECIAL-USE QUOTA ACL RIGHTS=texk] Logged in
然而,当我在Go语言中尝试同样的方法时,我只从IMAP服务器得到了登录前的消息。
func main() {
conn, err := tls.Dial("tcp", "mail.privateemail.com:993", nil)
if err != nil {
log.Println(err.Error())
}
defer conn.Close()
n, err := conn.Write([]byte("test LOGIN \"myuser@email.com\" \"myp4ssw0rd\"\n"))
if err != nil {
log.Println(n, err.Error())
}
buf := make([]byte, 1000)
n, err = conn.Read(buf)
if err != nil {
log.Println(n, err)
return
}
log.Println(string(buf))
}
它将生成以下输出:
go run main.go
2023/02/09 10:06:12 * OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ AUTH=PLAIN AUTH=LOGIN] Dovecot ready.
要使用*tls.Conn
向IMAP服务器发送命令,我需要做什么?
1条答案
按热度按时间emeijp431#
https://www.rfc-editor.org/rfc/rfc3501#section-2.2
客户端和服务器传输的所有交互都是行的形式,即以CRLF结尾的字符串。
conn.Write([]byte("test LOGIN \"myuser@email.com\" \"myp4ssw0rd\"\r\n"))