-
Notifications
You must be signed in to change notification settings - Fork 111
/
Examples.xml
374 lines (304 loc) · 12.3 KB
/
Examples.xml
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<S22>
<Imap>
<ImapClient name="ctor-1">
<example>
This example shows how to establish a connection with an IMAP server
and print out the IMAP options, which the server supports.
<code>
// Connect to Gmail's IMAP server on port 993 using SSL.
ImapClient Client = new ImapClient("imap.gmail.com", 993, true);
// Print out the server's capabilities.
foreach(string s in Client.Capabilities())
Console.WriteLine(s);
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="ctor-2">
<example>
This example demonstrates how to connect and login to an IMAP server.
<code>
// Connect to Gmail's IMAP server on port 993 using SSL.
ImapClient Client = null;
try {
Client = new ImapClient("imap.gmail.com", 993, "My_Username",
"My_Password", true, AuthMethod.Auto);
// Check if the server supports IMAP IDLE.
if(Client.Supports("IDLE"))
Console.WriteLine("This server supports the IMAP4 IDLE specification");
else
Console.WriteLine("This server does not support IMAP IDLE");
}
catch(InvalidCredentialsException) {
Console.WriteLine("The server rejected the supplied credentials");
}
finally {
// Release resources.
if(Client != null)
Client.Dispose();
}
</code>
</example>
</ImapClient>
<ImapClient name="Login">
<example>
This example demonstrates how to authenticate with an IMAP server once a connection
has been established. Notice that you can also connect and login in one step
using one of the overloaded constructors.
<code>
// Connect to Gmail's IMAP server on port 993 using SSL.
ImapClient Client = new ImapClient("imap.gmail.com", 993, true);
try {
Client.Login("My_Username", "My_Password", AuthMethod.Auto);
}
catch(InvalidCredentialsException) {
Console.WriteLine("The server rejected the supplied credentials.");
}
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="Search">
<example>
This example demonstrates how to use the search method to get a list of all
unread messages in the mailbox.
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_Username",
"My_Password", true, AuthMethod.Login);
// Get a list of unique identifiers (UIDs) of all unread messages in the mailbox.
IEnumerable<uint> uids = Client.Search( SearchCondition.Unseen() );
// Fetch the messages and print out their subject lines.
foreach(uint uid in uids) {
MailMessage message = Client.GetMessage(uid);
Console.WriteLine(message.Subject);
}
// Free up any resources associated with this instance.
Client.Dispose();
</code>
</example>
<example>
This example demonstrates how to perform a search using multiple search criteria.
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_Username",
"My_Password", true, AuthMethod.Login);
// Get a list of unique identifiers (UIDs) of all messages sent before the 01.04.2012
// and that are larger than 1 Kilobyte.
IEnumerable<uint> uids = Client.Search( SearchCondition.SentBefore(new DateTime(2012, 4, 1))
.And( SearchCondition.Larger(1024) ));
Console.WriteLine("Found " + uids.Count() + " messages");
// Free up any resources associated with this instance.
Client.Dispose();
</code>
</example>
<example>
The following example demonstrates how to perform a search using characters outside
the ASCII range.
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_Username",
"My_Password", true, AuthMethod.Login);
// Get a list of unique identifiers (UIDs) of all messages that have
// the Japanese expression "フーリエ変換" in their subject lines.
try {
IEnumerable<uint> uids = Client.Search(SearchCondition.Subject("フーリエ変換"));
Console.WriteLine("Found " + uids.Count() + " messages");
} catch(NotSupportedException e) {
// If this exception is thrown, the server does not support searching for characters
// outside the ASCII range.
Console.WriteLine("The server does not support searching for non-ASCII values.");
}
// Free up any resources associated with this instance.
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="GetMessage-1">
<example>
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
// Find all messages in the mailbox that were sent from "[email protected]".
IEnumerable<uint> uids = Client.Search( SearchCondition.From("[email protected]") );
// Fetch the first message and print it's subject and body.
if(uids.Count() > 0) {
MailMessage msg = Client.GetMessage(uids.First());
Console.WriteLine("Subject: " + msg.Subject);
Console.WriteLine("Body: " + msg.Body);
}
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="GetMessage-2">
<example>
This example demonstrates how to fetch only the mail message headers of
a mail message, instead of the entire message.
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
// Find all messages in the mailbox that have "Hello" in the subject.
IEnumerable<uint> uids = Client.Search( SearchCondition.Subject("Hello") );
// Fetch the mail headers of the first message and print it's subject line.
if(uids.Count() > 0) {
MailMessage msg = Client.GetMessage(uids.First(), FetchOptions.HeadersOnly);
Console.WriteLine("Subject: " + msg.Subject);
}
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="GetMessage-3">
<example>
This example demonstrates how to use the ExaminePartDelegate with the GetMessage
method to only download message parts with a size of 1 Megabyte or less.
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
// Find all messages in the inbox.
IEnumerable<uint> uids = Client.Search( SearchCondition.All() );
// Download each message but skip message parts that are larger than 1 Megabyte.
foreach(uint uid in uids) {
MailMessage msg = Client.GetMessage(uid, (Bodypart part) => {
if(part.Size > (1024 * 1024))
return false;
else
return true;
}
);
}
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="GetMessages-1">
<example>
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
// Find all messages that have been sent since June the 1st.
IEnumerable<uint> uids = Client.Search( SearchCondition.SentSince( new DateTime(2012, 6, 1) ) );
// Fetch the messages and print out their subject lines.
IEnumerable<MailMessage> messages = Client.GetMessages( uids );
foreach(MailMessage m in messages)
Console.WriteLine("Subject: " + m.Subject);
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="GetMessages-2">
<example>
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
// Find all messages that have been sent since June the 1st.
IEnumerable<uint> uids = Client.Search( SearchCondition.SentSince( new DateTime(2012, 6, 1) ) );
// Retrieve the messages and print out their subject lines. If any of the messages are multipart
// messages, only those parts, that have a content-type of text will be fetched.
IEnumerable<MailMessage> messages = Client.GetMessages( uids, FetchOptions.TextOnly );
foreach(MailMessage m in messages)
Console.WriteLine("Subject: " + m.Subject);
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="GetMailboxInfo">
<example>
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
// Get a list of all mailboxes.
foreach(string m in Client.ListMailboxes())
{
MailboxInfo info = Client.GetMailboxInfo(m);
Console.WriteLine(info.Name);
Console.WriteLine("Used storage: " + info.UsedStorage);
Console.WriteLine("Free storage: " + info.FreeStorage);
Console.WriteLine("Next UID: " + info.NextUID);
Console.WriteLine("Messages: " + info.Messages);
Console.WriteLine("Unread: " + info.Unread);
Console.WriteLine("Set Flags: ");
foreach (MailboxFlag f in info.Flags)
Console.Write(f.ToString() + ",");
Console.WriteLine();
}
Client.Dispose();
</code>
</example>
</ImapClient>
<ImapClient name="NewMessage">
<example>
This example demonstrates how to receive IMAP IDLE notifications.
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
// Make sure our server actually supports IMAP IDLE.
if(!Client.Supports("IDLE"))
throw new Exception("This server does not support IMAP IDLE");
// Our event handler will be called whenever a new message is received
// by the server.
Client.NewMessage += new EventHandler<IdleMessageEventArgs>(OnNewMessage);
// .........
Client.Dispose();
// ........
void OnNewMessage(object sender, IdleMessageEventArgs e) {
Console.WriteLine("Received a new message!");
Console.WriteLine("Total number of messages in the mailbox: " +
e.MessageCount);
}
</code>
</example>
</ImapClient>
<ImapClient name="MessageDeleted">
<example>
This example demonstrates how to receive IMAP IDLE notifications.
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
// Make sure our server actually supports IMAP IDLE.
if(!Client.Supports("IDLE"))
throw new Exception("This server does not support IMAP IDLE");
// Our event handler will be called whenever a message is deleted on the server.
Client.MessageDeleted += new EventHandler<IdleMessageEventArgs>(OnMessageDeleted);
// .........
// Don't forget to dispose the client once you're done with it.
Client.Dispose();
// ........
void OnMessageDeleted(object sender, IdleMessageEventArgs e) {
Console.WriteLine("A mail message was deleted on the server!");
Console.WriteLine("Total number of mail messages in the mailbox: " +
e.MessageCount);
}
</code>
</example>
</ImapClient>
<ImapClient name="StoreMessage">
<example>
This example demonstrates how to store a mail message on an IMAP server.
<code>
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_UsernamMe",
"My_Password", true, AuthMethod.Login);
MailMessage message = CreateSimpleMailMessage();
uint uid = Client.StoreMessage(message);
Console.WriteLine("The UID of the stored mail message is " + uid);
Client.Dispose();
// ...........
// This creates a simple mail message with a text/plain body and a PNG image
// as a file attachment.
// Consult the MSDN website for more details on the System.Net.Mail.Mailmessage class.
static MailMessage CreateSimpleMailMessage() {
MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");
message.To.Add("[email protected]");
message.Subject = "This is just a test!";
message.Body = "This is the text/plain body. An additional HTML body " +
"can optionally be attached as an alternate view";
// Add the attachment.
Attachment attachment = new Attachment("some_image.png", "image/png");
attachment.Name = "my_attached_image.png";
message.Attachments.Add(attachment);
return message;
}
</code>
</example>
</ImapClient>
</Imap>
</S22>