Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
368 lines (299 sloc) 12 KB
<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 */
try {
ImapClient Client = new ImapClient("imap.gmail.com", 993, "My_Username",
"My_Password", true, AuthMethod.Login);
/* 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");
/* release resources */
Client.Dispose();
}
catch(InvalidCredentialsException) {
Console.WriteLine("The server rejected the supplied credentials");
}
</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.Login);
}
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 */
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 */
uint[] uids = Client.Search( SearchCondition.SentBefore(new DateTime(2012, 4, 1))
.And( SearchCondition.Larger(1024) ));
Console.WriteLine("Found " + uids.Length + " 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 {
uint[] uids = Client.Search(SearchCondition.Subject("フーリエ変換"));
Console.WriteLine("Found " + uids.Length + " 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 "John.Doe@gmail.com" */
uint uids = Client.Search( SearchCondition.From("John.Doe@gmail.com") );
/* fetch the first message and print it's subject and body */
if(uids.Length > 0) {
MailMessage msg = Client.GetMessage(uids[0]);
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 */
uint uids = Client.Search( SearchCondition.Subject("Hello") );
/* fetch the mail headers of the first message and print it's subject line */
if(uids.Length > 0) {
MailMessage msg = Client.GetMessage(uids[0], 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 */
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(uids[0], (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 */
uint uids = Client.Search( SearchCondition.SentSince( new DateTime(2012, 6, 1) ) );
/* fetch the messages and print out their subject lines */
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 */
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. */
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&lt;IdleMessageEventArgs&gt;(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&lt;IdleMessageEventArgs&gt;(OnMessageDeleted);
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 details on the System.Net.Mail.Mailmessage class
*/
static MailMessage CreateSimpleMailMessage() {
MailMessage message = new MailMessage();
message.From = new MailAddress("someone@someplace.com");
message.To.Add("john.doe@someplace.com");
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>