-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractor.cs
148 lines (124 loc) · 4.21 KB
/
Extractor.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Chilkat;
using Task = System.Threading.Tasks.Task;
namespace gmail_imap
{
internal class Extractor
{
public string Host { get; set; }
public int Port { get; set; }
public string Account { get; set; }
public string Username { get; set; }
public Extractor(string host, int port, string account)
{
Host = host;
Port = port;
Account = account;
}
private Imap Connect()
{
Imap imap = new()
{
Ssl = true,
Port = Port
};
bool success = imap.Connect(Host);
if (!success)
{
throw new Exception(imap.LastErrorText);
}
return imap;
}
private Imap Auth()
{
var imap = Connect();
var account = Account.Split(new char[] { ',', ':', ';'});
if(account.Length != 2)
{
throw new Exception("Invalid account format");
}
Username = account[0].Split('@')[0];
var success = imap.Login(account[0], account[1]);
if (!success)
{
throw new Exception(imap.LastErrorText);
}
return imap;
}
public async Task Extract_Froms()
{
var imap = Auth();
var success = imap.SelectMailbox("Inbox");
if (!success)
{
throw new Exception(imap.LastErrorText);
}
var messageSet = imap.Search("ALL", true);
if (imap.LastMethodSuccess != true)
{
throw new Exception(imap.LastErrorText);
}
var bundle = imap.FetchHeaders(messageSet);
List<string> froms = new();
for (int i = 0; i < bundle.MessageCount; i++)
{
var email = bundle.GetEmail(i);
var from = email.FromAddress;
if(!string.IsNullOrEmpty(from))
{
froms.Add(from);
}
}
//disconnet
imap.Disconnect();
//remove duplicate
froms = froms.Distinct().ToList();
if(froms.Count != 0)
{
await File.AppendAllTextAsync(Username + "_froms.txt", string.Join('\n', froms));
}
}
public async Task Extract_Bounce_Emails()
{
var imap = Auth();
var success = imap.SelectMailbox("Inbox");
if (!success)
{
throw new Exception(imap.LastErrorText);
}
var last30days = DateTime.Now.AddDays(-30).ToString("dd-MMM-yyyy");
var messageSet = imap.Search($"FROM \"[email protected]\" SINCE {last30days}", true);
if (imap.LastMethodSuccess != true)
{
throw new Exception(imap.LastErrorText);
}
var bundle = imap.FetchBundle(messageSet);
List<string> bounces = new();
for (int i = 0; i < bundle.MessageCount; i++)
{
var email = bundle.GetEmail(i);
var from = email.FromAddress;
if (from == "[email protected]")
{
// Check if body contains "?p=NoSuchUser"
if (email.Body.Contains("?p=NoSuchUser"))
{
// Extract the "X-Failed-Recipients" header
var failedRecipients = email.GetHeaderField("X-Failed-Recipients");
if (!string.IsNullOrEmpty(failedRecipients))
{
bounces.Add(failedRecipients);
}
}
}
}
// Disconnect
imap.Disconnect();
// Remove duplicates
bounces = bounces.Distinct().ToList();
if (bounces.Count != 0)
{
await File.AppendAllTextAsync(Username + "_bounces.txt", string.Join('\n', bounces));
}
}
}
}