Problem Description:
Recently, 163 emails received by golang reported errors, select inbox err: SELECT Unsafe Login. Please contact [email protected] for help.
reason
This is due to the need to add ID information
[see agreement](https://www.ietf.org/rfc/rfc2971.txt “Agreement” (see)
terms of settlement
Add ID information using go IMAP extensions go IMAP ID
The code is as follows
import id "github.com/emersion/go-imap-id"
idClient := id.NewClient(c)
idClient.ID(
id.ID{
id.FieldName: "IMAPClient",
id.FieldVersion: "3.1.0",
},
)
Complete incoming mail code
package main
import (
"fmt"
net_mail "net/mail"
"studyGo/emailT/tools"
"time"
"github.com/emersion/go-imap"
id "github.com/emersion/go-imap-id"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-message"
)
func loginEmail(Eserver, UserName, Password string) (*client.Client, error) {
c, err := client.DialTLS(Eserver, nil)
if err != nil {
return nil, err
}
if err = c.Login(UserName, Password); err != nil {
return nil, err
}
return c, nil
}
func emailList(Eserver, UserName, Password string) (err error) {
c, err := loginEmail(Eserver, UserName, Password)
if err != nil {
fmt.Println(err)
return
}
idClient := id.NewClient(c)
idClient.ID(
id.ID{
id.FieldName: "IMAPClient",
id.FieldVersion: "3.1.0",
},
)
defer c.Close()
mbox, err := c.Select("INBOX", false)
if err != nil {
fmt.Println("select inbox err: ", err)
return
}
if mbox.Messages == 0 {
return
}
criteria := imap.NewSearchCriteria()
t1, err := time.Parse("2006-01-02 15:04:05", "2020-03-02 15:04:05")
criteria.Since = t1
ids, err := c.Search(criteria)
if err != nil {
fmt.Println(err)
}
if len(ids) == 0 {
return
}
seqset := new(imap.SeqSet)
seqset.AddNum(ids...)
sect := &imap.BodySectionName{}
messages := make(chan *imap.Message, 100)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, []imap.FetchItem{sect.FetchItem()}, messages)
}()
for msg := range messages {
r := msg.GetBody(sect)
m, err := message.Read(r)
if err != nil {
fmt.Println(err)
// return err
}
header := m.Header
emailDate, _ := net_mail.ParseDate(header.Get("Date"))
subject := tools.GetSubject(header)
from := tools.GetFrom(header)
// body, _ := tools.ParseBody(m.Body)
fmt.Printf("%s in the time of :%v send an email as:%s \n", from, emailDate, subject)
}
return
}