forked from andyedinborough/aenetmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pop3Client.cs
80 lines (66 loc) · 2.67 KB
/
Pop3Client.cs
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
using System;
using System.Text;
namespace AE.Net.Mail {
public class Pop3Client : TextClient, IMailClient {
public Pop3Client() { }
public Pop3Client(string host, string username, string password, int port = 110, bool secure = false) {
Connect(host, port, secure);
Login(username, password);
}
internal override void OnLogin(string username, string password) {
SendCommandCheckOK("USER " + username);
SendCommandCheckOK("PASS " + password);
}
internal override void OnLogout() {
SendCommand("QUIT");
}
internal override void CheckResultOK(string result) {
if (!result.StartsWith("+OK", StringComparison.OrdinalIgnoreCase)) {
throw new Exception(result.Substring(result.IndexOf(' ') + 1).Trim());
}
}
public int GetMessageCount() {
CheckConnectionStatus();
var result = SendCommandGetResponse("STAT");
CheckResultOK(result);
return int.Parse(result.Split(' ')[1]);
}
//public string[] GetMessageIDs() {
// CheckConnectionStatus();
// string result = SendCommandGetResponse("LIST");
// List<string> ids = new List<string>();
// while (result != ".") {
// result = _Reader.ReadLine();
// int i = result.IndexOf(' ');
// if (i > -1)
// ids.Add(result.Substring(0, i + 1));
// }
// return ids.ToArray();
//}
public MailMessage GetMessage(int index, bool headersOnly = false) {
return GetMessage((index + 1).ToString(), headersOnly);
}
public MailMessage GetMessage(string uid, bool headersOnly = false) {
CheckConnectionStatus();
var result = new StringBuilder();
string line = SendCommandGetResponse(string.Format(headersOnly ? "TOP {0} 0" : "RETR {0}", uid));
while (line != ".") {
result.AppendLine(line);
line = _Reader.ReadLine();
}
var msg = new MailMessage();
msg.Load(result.ToString(), headersOnly);
msg.Uid = uid;
return msg;
}
public void DeleteMessage(string uid) {
SendCommandCheckOK("DELE " + uid);
}
public void DeleteMessage(int index) {
DeleteMessage((index + 1).ToString());
}
public void DeleteMessage(AE.Net.Mail.MailMessage msg) {
DeleteMessage(msg.Uid);
}
}
}