Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
374 lines (304 sloc) 12.3 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.
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&lt;uint&gt; 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&lt;uint&gt; 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&lt;uint&gt; 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 "John.Doe@gmail.com".
IEnumerable&lt;uint&gt; uids = Client.Search( SearchCondition.From("John.Doe@gmail.com") );
// 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&lt;uint&gt; 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&lt;uint&gt; 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&lt;uint&gt; uids = Client.Search( SearchCondition.SentSince( new DateTime(2012, 6, 1) ) );
// Fetch the messages and print out their subject lines.
IEnumerable&lt;MailMessage&gt; 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&lt;uint&gt; 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&lt;MailMessage&gt; 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);
// .........
// 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("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>