-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
104 lines (86 loc) · 2.43 KB
/
example_test.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
102
103
104
// Copyright 2013 The Go-IMAP Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the go-imap.LICENSE file.
package imap_test
import (
"bytes"
"fmt"
"net/mail"
"time"
"github.com/xarg/imap"
)
func ExampleClient() {
//
// Note: most of error handling code is omitted for brevity
//
var (
c *imap.Client
cmd *imap.Command
rsp *imap.Response
)
// Connect to the server
c, _ = imap.Dial("imap.example.com")
// Remember to log out and close the connection when finished
defer c.Logout(30 * time.Second)
// Print server greeting (first response in the unilateral server data queue)
fmt.Println("Server says hello:", c.Data[0].Info)
c.Data = nil
// Enable encryption, if supported by the server
if c.Caps["STARTTLS"] {
c.StartTLS(nil)
}
// Authenticate
if c.State() == imap.Login {
c.Login("[email protected]", "mysupersecretpassword")
}
// List all top-level mailboxes, wait for the command to finish
cmd, _ = imap.Wait(c.List("", "%"))
// Print mailbox information
fmt.Println("\nTop-level mailboxes:")
for _, rsp = range cmd.Data {
fmt.Println("|--", rsp.MailboxInfo())
}
// Check for new unilateral server data responses
for _, rsp = range c.Data {
fmt.Println("Server data:", rsp)
}
c.Data = nil
// Open a mailbox (synchronous command - no need for imap.Wait)
c.Select("INBOX", true)
fmt.Print("\nMailbox status:\n", c.Mailbox)
// Fetch the headers of the 10 most recent messages
set, _ := imap.NewSeqSet("")
if c.Mailbox.Messages >= 10 {
set.AddRange(c.Mailbox.Messages-9, c.Mailbox.Messages)
} else {
set.Add("1:*")
}
cmd, _ = c.Fetch(set, "RFC822.HEADER")
// Process responses while the command is running
fmt.Println("\nMost recent messages:")
for cmd.InProgress() {
// Wait for the next response (no timeout)
c.Recv(-1)
// Process command data
for _, rsp = range cmd.Data {
header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])
if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
fmt.Println("|--", msg.Header.Get("Subject"))
}
}
cmd.Data = nil
// Process unilateral server data
for _, rsp = range c.Data {
fmt.Println("Server data:", rsp)
}
c.Data = nil
}
// Check command completion status
if rsp, err := cmd.Result(imap.OK); err != nil {
if err == imap.ErrAborted {
fmt.Println("Fetch command aborted")
} else {
fmt.Println("Fetch error:", rsp.Info)
}
}
}