emersion/go-imap - How to retrieve and list unseen messages

1,044 views
Skip to first unread message

Ivo Freitas

unread,
Feb 27, 2018, 8:11:58 AM2/27/18
to golang-nuts
Hello

I'm trying to use this implementation of IMAP protocol mas seems to be impossible to retrieve unseen messages from the mailbox.
Here's my code.

package main

import (
"emailmonitor/util"
"fmt"
)

func main() {

serverGmail := util.NewServerGmail()

serverGmail.Connect()
serverGmail.Login()
serverGmail.ListUnseenMessages()

}

//-----------------------------------------

package util

import (
"io/ioutil"
"log"
"net/mail"
"net/smtp"

)

type ServerGmail struct {
user string
pass string
erro string
cliente *client.Client
}

func NewServerGmail() *ServerGmail {
serverGmail := &ServerGmail{}
serverGmail.user = "xxx...@gmail.com"
serverGmail.pass = "xxxxx"
serverGmail.erro = ""

return serverGmail
}

func (serverGmail *ServerGmail) Connect() {
// Connect to server
cliente, erro := client.DialTLS("smtp.gmail.com:993", nil)
if erro != nil {
serverGmail.erro = erro.Error()
}
log.Println("Connected")

serverGmail.cliente = cliente

}

func (serverGmail *ServerGmail) Login() {
// Login
if erro := serverGmail.cliente.Login(serverGmail.user, serverGmail.pass); erro != nil {
serverGmail.erro = erro.Error()
}
log.Println("Logged")

}

func (serverGmail *ServerGmail) setLabelBox(label string) *imap.MailboxStatus {
mailbox, erro := serverGmail.cliente.Select(label, true)
if erro != nil {
serverGmail.erro = erro.Error()
}
return mailbox
}

func (serverGmail *ServerGmail) ListUnseenMessages() {
mailbox := serverGmail.setLabelBox("INBOX")
log.Println(mailbox.Unseen)
}

This line log.Println(mailbox.Unseen) prints 0 (zero) messages and as described on doc's should be "The number of unread messages". I tried with UnseenSeqNum "The sequence number of the first unseen message in the mailbox" property and also prints 0 (zero).
Its just a start but I wonder from there, how could I retrieve the unseen messages.
Any help is welcome

Thank you

Ain

unread,
Feb 27, 2018, 8:17:10 AM2/27/18
to golang-nuts

teisipäev, 27. veebruar 2018 15:11.58 UTC+2 kirjutas Ivo Freitas:
Hello

I'm trying to use this implementation of IMAP protocol mas seems to be impossible to retrieve unseen messages from the mailbox.

Reply all
Reply to author
Forward
0 new messages