Rebex Mail Pack
IMAP, POP3, SMTP, EWS, MSG, MS Graph, MIME, S/MIME libraries for .NET
Download 30-day free trial Buy from $299More .NET libraries
-
Rebex File Transfer Pack
FTP/SSL and SFTP together
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Easy-to-use API
On this page:
Sending email using SMTP
A typical SMTP session goes through the following steps:
- Connect to an SMTP server.
- Validate the server certificate (this is done automatically by default, but you can perform the validation yourself).
- Log in - if required, authenticate with a user name and password or using a certificate.
- Send e-mail messages, etc.
- Disconnect.
The following code sends a simple mail message:
string sender = "my_account@gmail.com"; string recipient = "to@example.org"; string subject = "Test"; string body = "Hello World!"; // create SMTP client instance using (var smtp = new Rebex.Net.Smtp()) { // connect to Gmail SMTP server smtp.Connect(gmail_smtp_hostname, SslMode.Explicit); // authenticate with your email address and password smtp.Login(sender, password); // send mail smtp.Send(sender, recipient, subject, body); // disconnect (not required, but polite) smtp.Disconnect(); }
Dim sender As String = "my_account@gmail.com" Dim recipient As String = "to@example.org" Dim subject As String = "Test" Dim body As String = "Hello World!" ' create SMTP client instance Using smtp = New Rebex.Net.Smtp() ' connect to Gmail SMTP server smtp.Connect("smtp.gmail.com", SslMode.Explicit) ' authenticate with your email address and password smtp.Login(sender, password) ' send mail smtp.Send(sender, recipient, subject, body) ' disconnect (not required, but polite) smtp.Disconnect() End Using
Sending e-mail can be even easier. Check out Sending e-mail with a single line of code.
Getting list of unread emails using IMAP
A typical IMAP session goes through the following steps:
- Connect to an IMAP server.
- Validate the server certificate (this is done automatically by default, but you can perform the validation yourself).
- Log in - if required, authenticate with a user name and password or using a certificate.
- Select a folder, find email messages, download messages, etc.
- Disconnect.
The following code finds all unread e-mail messages and prints information about each of them:
// create IMAP client instance using (var imap = new Rebex.Net.Imap()) { // connect to Gmail IMAP server imap.Connect("imap.gmail.com", SslMode.Implicit); // authenticate imap.Login(username, password); // select folder to work with imap.SelectFolder("INBOX"); // get list of unread messages ImapMessageCollection list = imap.Search(ImapSearchParameter.Unread); // print some info foreach (ImapMessageInfo info in list) { Console.WriteLine("From: {0}", info.From); Console.WriteLine("To: {0}", info.To); Console.WriteLine("Subject: {0}", info.Subject); } // disconnect (not required, but polite) imap.Disconnect(); }
' create IMAP client instance Using imap = New Rebex.Net.Imap() ' connect to Gmail IMAP server imap.Connect("imap.gmail.com", SslMode.Implicit) ' authenticate imap.Login(username, password) ' select folder to work with imap.SelectFolder("INBOX") ' get list of unread messages Dim list As ImapMessageCollection = imap.Search(ImapSearchParameter.Unread) ' print some info For Each info As ImapMessageInfo In list Console.WriteLine("From: {0}", info.From) Console.WriteLine("To: {0}", info.To) Console.WriteLine("Subject: {0}", info.Subject) Next ' disconnect (not required, but polite) imap.Disconnect() End Using
Note: Although implicit SSL mode has been discouraged since 1999 (see RFC 2595) Gmail and some other providers still only supports this mode. Explicit mode should be used instead when available.
Getting list of unread emails using Exchange Web Services (EWS)
A typical EWS (Exchange Web Services) session goes through the following steps:
- Connect to an Exchange server.
- Validate the server certificate (this is done automatically by default, but you can perform the validation yourself).
- Log in - if required, authenticate with a user name and password or using a certificate.
- Select a folder, find email messages, download messages, etc.
- Disconnect.
The following code finds all unread e-mail messages and prints information about each of them:
// create EWS client instance using (var client = new Rebex.Net.Ews()) { // connect to Exchange server client.Connect(hostname, SslMode.Implicit); // authenticate client.Login(username, password); // to select folder to work with use either: // - client.FindFolder(EwsFolderId.Root, "folderName") // - predefined folder id var folder = EwsFolderId.Inbox; // get list of unread messages var list = client.Search( folder, EwsSearchParameter.IsRead(false)); // print some info foreach (EwsMessageInfo messageInfo in list) { Console.WriteLine("From: {0}", messageInfo.From); Console.WriteLine("To: {0}", messageInfo.To); Console.WriteLine("Subject: {0}", messageInfo.Subject); } // disconnect (not required, but polite) client.Disconnect(); }
' create IMAP client instance Using client = New Rebex.Net.Ews() ' connect to Exchange server client.Connect(hostname, SslMode.Implicit) ' authenticate client.Login(username, password) ' to select folder to work with use either ' - client.FindFolder(EwsFolderId.Root, "folderName") ' - predefined folder id Dim folder = EwsFolderId.Inbox ' get list of unread messages Dim list As EwsMessageCollection = client.Search( folder, EwsSearchParameter.IsRead(False)) ' print some info For Each messageInfo As EwsMessageInfo In list Console.WriteLine("From: {0}", messageInfo.From) Console.WriteLine("To: {0}", messageInfo.To) Console.WriteLine("Subject: {0}", messageInfo.Subject) Next ' disconnect (not required, but polite) client.Disconnect() End Using
Getting list of unread emails using Microsoft Graph API
A typical Graph session goes through the following steps:
- Connect to Exchange Online (Microsoft 365) server.
- Authenticate using an OAuth access token.
- Find e-mail messages, download e-mails, send e-mails, or more.
- Disconnect.
The following code searches for unread e-mail messages and shows information about them:
// create Graph client instance using (var client = new Rebex.Net.GraphClient()) { // connect to Exchange Online (Microsoft 365) server client.Connect(); // authenticate with your OAuth access token client.Login(token); // get first page of a list of unread messages from the 'Inbox' folder var unread = GraphMessageSearchParameter.IsRead(false); GraphMessageCollection messages = client.Search(GraphFolderId.Inbox, unread); // print some info foreach (GraphMessageInfo info in messages) { Console.WriteLine("Subject: {0}", info.Subject); Console.WriteLine("From: {0}", info.From); Console.WriteLine("To: {0}", info.To); } // disconnect (not required, but polite) client.Disconnect(); }
Note: This code returns the first page of the results. See Paging results for more information about paging.
Downloading emails using POP3
A typical POP3 session goes through the following steps:
- Connect to a POP3 server.
- Validate the server certificate (this is done automatically by default, but you can perform the validation yourself).
- Log in - if required, authenticate with a user name and password or using a certificate.
- Download emails, delete emails, etc.
- Disconnect.
The following code downloads all emails:
// create POP3 client instance using (var pop3 = new Rebex.Net.Pop3()) { // connect to Gmail POP3 server pop3.Connect("pop.gmail.com", SslMode.Implicit); // authenticate pop3.Login(username, password); // get message list Pop3MessageCollection list = pop3.GetMessageList(); // print some info Console.WriteLine("{0} email(s) found.", list.Count); // download all messages foreach (Pop3MessageInfo info in list) { MailMessage mail = pop3.GetMailMessage(info.SequenceNumber); // do something with the mail // ... } // disconnect (required to commit delete operations) pop3.Disconnect(); }
' create POP3 client instance Using pop3 = New Rebex.Net.Pop3() ' connect to Gmail POP3 server pop3.Connect("pop.gmail.com", SslMode.Implicit) ' authenticate pop3.Login(username, password) ' get message list Dim list As Pop3MessageCollection = pop3.GetMessageList() ' print some info Console.WriteLine("{0} email(s) found.", list.Count) ' download all messages For Each info As Pop3MessageInfo In list ' do something with the mail ' ... Dim mail As MailMessage = pop3.GetMailMessage(info.SequenceNumber) Next ' disconnect (required to commit delete operations) pop3.Disconnect() End Using
Note: Although implicit SSL mode has been discouraged since 1999 (see RFC 2595) Gmail and some other providers still only supports this mode. Explicit mode should be used instead when available.
Loading and saving Outlook mail messages using MSG
To load a message, use MsgMessage.Load()
method.
To save a message use MsgMessage.Save()
method.
Both methods accept path or Stream
arguments.
// create an instance of MsgMessage var message = new MsgMessage(); // load a message from file message.Load(@"C:\MyData\sample.msg"); // .. work with the message // save the message into a memory stream for later use var stream = new MemoryStream(); message.Save(stream); stream.Position = 0;
' create an instance of MsgMessage Dim message = New MsgMessage() ' load a message from file message.Load("C:\MyData\sample.msg") ' .. work with the message ' save the message into a memory stream for later use Dim stream = New MemoryStream() message.Save(stream) stream.Position = 0
Back to feature list...