Rebex Secure Mail
SMTP, IMAP, EWS, POP3, S/MIME .NET library
Download 30-day free trial Buy from $299More .NET components
-
Rebex FTP/SSL
.NET FTP client
-
Rebex MSG
Outlook MSG file format library
-
Rebex Total Pack
All Rebex components 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("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(); }
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 (var 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
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.
Back to feature list...