Author Archives: ComponentPro Software

How to List Messages

Both ImapClient and Pop3Client allow you to retrieve a list of messages on the server with methods named ListMessages.With the Pop3Client, you can pass an optional flag parameter indicating which part of the message will be retrieved. The ListMessages method returns a collection of Pop3Message object containing information about messages you have requested as shown the following steps:

 

  • Add using directives to your code to create aliases for existing root namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:

    C#

    using ComponentSoft.Net.Mail;

    VB.NET

    Imports ComponentSoft.Net.Mail

  • Create a new instance of the Pop3Client class and connect to the POP3 server with Connect methods. The code looks similar to the following:
    C#

    // Create a new instance of the Pop3Client class.
    Pop3Client client = new Pop3Client();
    // Connect to the server.
    client.Connect(“myserver”);
    // Or you can specify the POP3 port with
    // client.Connect(“myserver”, 110);
    // Login to the server.
    client.Authenticate(“user”, “password”);
    // List messages. Only Size and UniqueId are retrieved.
    Pop3MessageCollection list = client.ListMessages(EnvelopeParts.Size | EnvelopeParts.UniqueId);
    foreach (Pop3Message m in list)
    {
    Console.WriteLine(
    string.Format(“UniqueId: {0}, Size: {1}”, m.UniqueId, m.Size));
    }
    // Close the connection.
    client.Disconnect();

    VB.NET

    ‘ Create a new instance of the Pop3Client class.
    Dim client As New Pop3Client()
    ‘ Connect to the server.
    client.Connect(“myserver”)
    ‘ Or you can specify the POP3 port with
    ‘ client.Connect(“myserver”, 110);
    ‘ Login to the server.
    client.Authenticate(“user”“password”)
    ‘ List messages. Only Size and UniqueId are retrieved.
    Dim list As Pop3MessageCollection = client.ListMessages(EnvelopeParts.Size OrEnvelopeParts.UniqueId)
    For Each m As Pop3Message In list
    Console.WriteLine(String.Format(“UniqueId: {0}, Size: {1}”, m.UniqueId, m.Size))
    Next m
    ‘ Close the connection.
    client.Disconnect()

How to use GMAIL Account to send emails

Explicit connection and Implicit connection are two secure methods to connect to a secure SMTP/SMTP/POP3 server. This topic will go in detail the Implicit connection.

Originally, a separate port was assigned to the SSL version of all these three protocols. These were 465 for SMTP/SSL, 993 for SMTP/SSL and 995 for POP3/SSL. Upon connection to this port, an SSL negotiation starts immediately and the control connection is secured. All data connections are also secured implicitly in the same way. This is similar to the approach used by HTTPS.

The code snippet below shows how to connect to the Gmail SMTP server securely using Implicit security mode and send an email using Ultimate Mail component:

C#

// Create a new instance of the SmtpClient class. 
SmtpClient client = new SmtpClient(); 
// Connect to the server. 
client.Connect("smtp.gmail.com", 465, SecurityMode.Implicit); 
// Login to the server. 
client.Authenticate("user", "password"); 
// Create a new mail message. 
MailMessage msg = new MailMessage(); 
msg.Subject = "Test Subject"; 
msg.BodyText = "Content"; 
msg.From = "from@mydomain.com"; 
msg.To = "to@somedomain.com"; 
// And send it. 
client.Send(msg); 
// Close the connection. 
client.Disconnect();

VB.NET

' Create a new instance of the SmtpClient class. 
Dim client As New SmtpClient() 
' Connect to the server. 
client.Connect("smtp.gmail.com", 465, SecurityMode.Implicit) 
' Login to the server. 
client.Authenticate("user", "password") 
' Create a new mail message. 
Dim msg As New MailMessage() 
msg.Subject = "Test Subject" 
msg.BodyText = "Content" 
msg.From = "from@mydomain.com" 
msg.To = "to@somedomain.com" 
' And send it. 
client.Send(msg) 
' Close the connection. 
client.Disconnect()

How to obtain POP3 mailbox information

The simplest way to obtain information about a specified mailbox is using GetMailboxInfo method. You only need to pass the mailbox name to the method. It returns a Pop3MailboxInfo object containing information about the mailbox you have requested.

The following steps will help you to do that using ComponentPro Mail component:

C#

// Create a new instance of the Pop3Client class. 
Pop3Client client = new Pop3Client(); 
// Connect to the server. 
client.Connect("myserver"); 
// Or you can specify the POP3 port with 
// client.Connect("myserver", 110); 
// Login to the server. 
client.Authenticate("user", "password"); 
// Obtain mailbox information. 
Pop3MailboxInfo info = client.GetMailboxInfo(); 
// Print out some information. 
Console.WriteLine("Number of messages found: {0}", info.MessageCount); 
Console.WriteLine("Mailbox Size: {0}", info.Size); 
// Close the connection. 
client.Disconnect();

VB.NET

' Create a new instance of the Pop3Client class. 
Dim client As New Pop3Client() 
' Connect to the server. 
client.Connect("myserver") 
' Or you can specify the POP3 port with 
' client.Connect("myserver", 110); 
' Login to the server. 
client.Authenticate("user", "password") 
' Obtain mailbox information. 
Dim info As Pop3MailboxInfo = client.GetMailboxInfo() 
' Print out some information. 
Console.WriteLine("Number of messages found: {0}", info.MessageCount) 
Console.WriteLine("Mailbox Size: {0}", info.Size) 
' Close the connection. 
client.Disconnect()

How to Download Mail Message Synchronously

Downloading mail messages from a IMAP server in UltimateMail component is so simple, you just call the DownloadMailMessage method and you retrieve an instance of the MailMessage class. You can now read, modify, save to disk the message. If you just need to download the message to disk or write it into a stream, just call the convenient DownloadMessage method instead.

The example below shows you how to get a list of messages from a IMAP server and download and save all messages to disk:

C#

// IMAP server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 993;
const SecurityMode securityMode = SecurityMode.Implicit;
// Create a new instance of the ImapClient class.
ImapClient client = new ImapClient();
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
client.Authenticate(user, password);
// Select 'INBOX' mailbox
client.Select("INBOX");
// Get the message list.
Console.WriteLine("Getting message list...");
ImapMessageCollection list = client.ListMessages(EnvelopeParts.UniqueId | EnvelopeParts.Size);
// Get messages.
for (int i = 0; i < list.Count; i++)
{
   ImapMessage imapMessage = list[i];
   // Download the message to an instance of the MailMessage class.
   MailMessage msg = client.DownloadMailMessage(imapMessage.UniqueId);
   // Display some information about it.
   Console.WriteLine("Size: " + imapMessage.Size);
   Console.WriteLine("Number of attachments: " + msg.Attachments.Count);
   Console.WriteLine("Number of header name value pairs: " + msg.Headers.Count);
}
// Close the connection.
client.Disconnect();

VB.NET

' IMAP server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 993
Const securityMode As SecurityMode = securityMode.Implicit
' Create a new instance of the ImapClient class.
Dim client As New ImapClient()
' Connect to the server.
client.Connect(serverName, port, securityMode)
' Login to the server.
client.Authenticate(user, password)
' Select 'INBOX' mailbox
client.Select("INBOX")
' Get the message list.
Console.WriteLine("Getting message list...")
Dim list As ImapMessageCollection = client.ListMessages(EnvelopeParts.UniqueId Or EnvelopeParts.Size)
' Get messages.
For i As Integer = 0 To list.Count - 1
    Dim imapMessage As ImapMessage = list(i)
    ' Download the message to an instance of the MailMessage class.
    Dim msg As MailMessage = client.DownloadMailMessage(imapMessage.UniqueId)
    ' Display some information about it.
    Console.WriteLine("Size: " & imapMessage.Size)
    Console.WriteLine("Number of attachments: " & msg.Attachments.Count)
    Console.WriteLine("Number of header name value pairs: " & msg.Headers.Count)
Next i
' Close the connection.
client.Disconnect()

After downloading messages from the server, you may want to obtain more information about the mailbox such as the number of unread messages and mailbox size, and then upload some messages to the server. See topic Obtaining account information for more details how to use the GetMailboxInfo method to get the account information.