示例#1
1
func main() {

	log.Printf("Connect to %s\n", serverAddress)

	var client *pop3.Client
	var dialErr error
	if secure {
		client, dialErr = pop3.DialTLS(serverAddress)
	} else {
		client, dialErr = pop3.Dial(serverAddress)
	}
	checkErr(dialErr)

	authErr := client.Auth(username, password)
	checkErr(authErr)

	log.Println("Fetching mailbox stat")
	cmdErr := PrintMailBoxStat(client)
	checkErr(cmdErr)

	log.Println("Downloading mails")
	err := ScanMailbox(client)
	checkErr(err)

	log.Println("Finished to download all of mails")

}
示例#2
0
func PrintMailBoxStat(client *pop3.Client) error {
	mailCount, mailBoxSize, err := client.Stat()
	if err != nil {
		return err
	}

	fmt.Printf("Account:%s, MailCount:%d, MailBoxSize:%s\n", username, mailCount, fileutil.Humanize(uint64(mailBoxSize)))
	return nil
}
示例#3
0
func ScanMailbox(client *pop3.Client) error {
	msgs, _, err := client.ListAll()
	checkErr(err)

	for i := offset; i < len(msgs); i++ {
		err = CheckAndDownloadMailContent(client, msgs[i])
		if err != nil {
			log.Fatalf("Download mail failure, Err:%v, Mail:%d, ", err, msgs[i])
		}
	}

	return nil
}
示例#4
0
func CheckAndDownloadMailContent(client *pop3.Client, index int) (err error) {
	filePath := filepath.Join(destination, strconv.Itoa(index))
	if _, errFileStat := os.Stat(filePath); errFileStat != nil { // mail file not found

		log.Printf("Downloading Mail:%d", index)
		// THINK: check content integrity
		var mailContent string
		mailContent, err = client.Retr(index)
		if err != nil {
			return
		}

		//find newline at head of file
		var lastNewline = 0
		for i, b := range mailContent {
			if b != '\n' {
				lastNewline = i
				break
			}
		}
		rawMailContent := []byte(mailContent)[lastNewline:]

		// it's safe to ignore parse error
		fromAddress, _ := getFromAddress(rawMailContent)

		if fromAddress == mailFrom {
			log.Printf("Store mail content to file:%s", filePath)
			err = ioutil.WriteFile(filePath, rawMailContent, 0644)
			if err != nil {
				return
			}
		}

	}
	return
}