Skip to content
Newer
Older
100644 288 lines (242 sloc) 7.89 KB
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
1 ### Examples
2
3 * [Connecting to an IMAP server using SSL](#1)
4 * [Download unseen mail messages](#2)
5 * [Search for messages](#3)
6 * [Figure out the amount of free space left for the inbox](#4)
7 * [Receive IMAP IDLE notifications](#5)
8 * [Download mail headers only instead of the entire mail message](#6)
9 * [Download attachments only if they are smaller than 2 Megabytes](#7)
10 * [Download attachments only if they are zip archives, otherwise skip them](#8)
92a6824 @smiley22 Added methods for storing messages
authored Sep 2, 2012
11 * [Store mail message on an IMAP server](#9)
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
12
13 <a name="1"></a>**Connecting to an IMAP server using SSL**
14
15 using System;
16 using S22.Imap;
17
18 namespace Test {
19 class Program {
20 static void Main(string[] args)
21 {
22 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
23 "username", "password", AuthMethod.Login, true))
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
24 {
25 Console.WriteLine("We are connected!");
26 }
27 }
28 }
29 }
30
31 <a name="2"></a>**Download unseen mail messages**
32
33 using System;
34 using S22.Imap;
35
36 namespace Test {
37 class Program {
38 static void Main(string[] args)
39 {
40 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
41 "username", "password", AuthMethod.Login, true))
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
42 {
b385ec7 @smiley22 Markdown woes
authored Jan 16, 2014
43 IEnumerable<uint> uids = Client.Search( SearchCondition.Unseen() );
44 IEnumerable<MailMessage> messages = Client.GetMessages(uids);
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
45 }
46 }
47 }
48 }
49
50 <a name="3"></a>**Search for messages**
51
52 using System;
53 using S22.Imap;
54
55 namespace Test {
56 class Program {
57 static void Main(string[] args)
58 {
59 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
60 "username", "password", AuthMethod.Login, true))
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
61 {
62 // Find messages that were sent from abc@def.com and have
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
63 // the string "Hello World" in their subject line.
b385ec7 @smiley22 Markdown woes
authored Jan 16, 2014
64 IEnumerable<uint> uids = Client.Search(
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
65 SearchCondition.From("abc@def.com").And(
66 SearchCondition.Subject("Hello World"))
67 );
68 }
69 }
70 }
71 }
72
73 <a name="4"></a>**Figure out the amount of free space left for the inbox**
74
75 *This is not supported by all IMAP servers and some may just return 0*
76
77 using System;
78 using S22.Imap;
79
80 namespace Test {
81 class Program {
82 static void Main(string[] args)
83 {
84 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
85 "username", "password", AuthMethod.Login, true))
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
86 {
87 MailboxStatus status = Client.GetStatus();
88
89 Console.WriteLine(status.FreeStorage + " Bytes left");
90 Console.WriteLine(status.UsedStorage + " Bytes used");
91 }
92 }
93 }
94 }
95
96 <a name="5"></a>**Receive IMAP IDLE notifications**
97
98 using System;
99 using S22.Imap;
100
101 namespace Test {
102 class Program {
103 static void Main(string[] args)
104 {
105 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
106 "username", "password", AuthMethod.Login, true))
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
107 {
108 // Should ensure IDLE is actually supported by the server
109 if(Client.Supports("IDLE") == false) {
110 Console.WriteLine("Server does not support IMAP IDLE");
111 return;
112 }
113
114 // We want to be informed when new messages arrive
115 Client.NewMessage += new EventHandler<IdleMessageEventArgs>(OnNewMessage);
116
117 // Put calling thread to sleep. This is just so the example program does
118 // not immediately exit.
119 System.Threading.Thread.Sleep(60000);
120 }
121 }
122
123 static void OnNewMessage(object sender, IdleMessageEventArgs e)
124 {
125 Console.WriteLine('A new message arrived. Message has UID: ' +
126 e.MessageUID);
127
128 // Fetch the new message's headers and print the subject line
129 MailMessage m = e.Client.GetMessage( e.MessageUID, FetchOptions.HeadersOnly );
130
131 Console.WriteLine("New message's subject: " + m.Subject);
132 }
133 }
134 }
135
136 <a name="6"></a>**Download mail headers only instead of the entire mail message**
137
138 using System;
139 using S22.Imap;
140
141 namespace Test {
142 class Program {
143 static void Main(string[] args)
144 {
145 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
146 "username", "password", AuthMethod.Login, true))
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
147 {
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
148 // This returns *ALL* messages in the inbox.
b385ec7 @smiley22 Markdown woes
authored Jan 16, 2014
149 IEnumerable<uint> uids = Client.Search( SearchCondition.All() );
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
150
151 // If we're only interested in the subject line or envelope
152 // information, just downloading the mail headers is alot
153 // cheaper and alot faster.
b385ec7 @smiley22 Markdown woes
authored Jan 16, 2014
154 IEnumerable<MailMessage> messages = Client.GetMessages(uids. FetchOptions.HeadersOnly);
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
155 }
156 }
157 }
158 }
159
160 <a name="7"></a>**Download attachments only if they are smaller than 2 Megabytes**
161
162 using System;
163 using S22.Imap;
164
165 namespace Test {
166 class Program {
167 static void Main(string[] args)
168 {
169 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
170 "username", "password", AuthMethod.Login, true))
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
171 {
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
172 // This returns all messages sent since August 23rd 2012.
b385ec7 @smiley22 Markdown woes
authored Jan 16, 2014
173 IEnumerable<uint> uids = Client.Search(
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
174 SearchCondition.SentSince( new DateTime(2012, 8, 23) )
175 );
176
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
177 // The expression will be evaluated for every MIME part
178 // of every mail message in the uids collection.
b385ec7 @smiley22 Markdown woes
authored Jan 16, 2014
179 IEnumerable<MailMessage> messages = Client.GetMessages(uids,
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
180 (Bodypart part) => {
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
181 // We're only interested in attachments.
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
182 if(part.Disposition.Type == ContentDispositionType.Attachment)
183 {
184 Int64 TwoMegabytes = (1024 * 1024 * 2);
185 if(part.Size > TwoMegabytes)
186 {
187 // Don't download this attachment
188 return false;
189 }
190 }
191
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
192 // Fetch MIME part and include it in the returned MailMessage instance.
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
193 return true;
194 }
195 );
196 }
197 }
198 }
199 }
200
201 <a name="8"></a>**Download attachments only if they are zip archives, otherwise skip them**
202
203 using System;
204 using S22.Imap;
205
206 namespace Test {
207 class Program {
208 static void Main(string[] args)
209 {
210 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
211 "username", "password", AuthMethod.Login, true))
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
212 {
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
213 // This returns all messages sent since August 23rd 2012.
b385ec7 @smiley22 Markdown woes
authored Jan 16, 2014
214 IEnumerable<uint> uids = Client.Search(
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
215 SearchCondition.SentSince( new DateTime(2012, 8, 23) )
216 );
217
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
218 // The expression will be evaluated for every MIME part
219 // of every mail message in the uids collection.
b385ec7 @smiley22 Markdown woes
authored Jan 16, 2014
220 IEnumerable<MailMessage> messages = Client.GetMessages(uids,
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
221 (Bodypart part) => {
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
222 // We're only interested in attachments.
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
223 if(part.Disposition.Type == ContentDispositionType.Attachment)
224 {
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
225 // Zip files have a content-type of application/zip.
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
226 if(part.Type == ContentType.Application &&
227 part.Subtype.toLower() == "zip")
228 {
229 return true;
230 }
231 else
232 {
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
233 // Skip this attachment, it's not a zip archive.
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
234 return false;
235 }
236 }
237
f7642a6 @smiley22 API changes, refactoring
authored Jan 16, 2014
238 // Fetch MIME part and include it in the returned MailMessage instance.
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
239 return true;
240 }
241 );
242 }
243 }
244 }
92a6824 @smiley22 Added methods for storing messages
authored Sep 2, 2012
245 }
246
247 <a name="9"></a>**Store mail message on an IMAP server**
248
249 using System;
250 using System.Net.Mail;
251 using S22.Imap;
252
253 namespace Test {
254 class Program {
255 static void Main(string[] args)
256 {
257 using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
d26730d @smiley22 Typo fix in Examples.md
authored Jan 11, 2014
258 "username", "password", AuthMethod.Login, true))
92a6824 @smiley22 Added methods for storing messages
authored Sep 2, 2012
259 {
260 MailMessage m = CreateSimpleMailMessage();
261
262 uint uid = Client.StoreMessage(m);
263 Console.WriteLine("The UID of the stored mail message is " + uid);
264 }
265 }
266
267 // This creates a simple mail message with a text/plain body and a PNG image
268 // as a file attachment.
269 // Consult the MSDN website for details on the System.Net.Mail.Mailmessage class
270 static MailMessage CreateSimpleMailMessage() {
271 MailMessage message = new MailMessage();
272
273 message.From = new MailAddress("someone@someplace.com");
274 message.To.Add("john.doe@someplace.com");
275
276 message.Subject = "This is just a test!";
277 message.Body = "This is the text/plain body. An additional HTML body " +
278 "can optionally be attached as an alternate view";
279
280 // Add the attachment
281 Attachment attachment = new Attachment("some_image.png", "image/png");
282 attachment.Name = "my_attached_image.png";
283 message.Attachments.Add(attachment);
284
285 return message;
286 }
287 }
c4122f9 @smiley22 Major rewrite of the message fetching code
authored Aug 25, 2012
288 }
Something went wrong with that request. Please try again.