Shopping Cart
|Contact|Obtain extended trial key
E-Mail Components Home

Mailit for .NET

The following code samples show how to send and receive messages

Mailit for .NET

Code Samples

The following code samples show how to send and receive messages with Mailit via SMTP, POP and IMAP.

Simple SMTP message sending

Now, when your project is been setup, you can use Mailit for .NET classes in your application. In this sample, we send a simple e-mail message with a few lines of code.

// Create session object
SmtpSession smtpSession = new SmtpSession(60);

// Establish connection and authenticate
smtpSession.Authenticate("mylogin", "mypassword");

// Create composer object and assign the text
MessageComposer messageComposer = new MessageComposer();
messageComposer.UnicodeText = "Message Text";

// Create contact list and assign a recipient
ContactList contactTo = new ContactList();
contactTo.AddContact("Joe", "");

// Create the sender address list
ContactList contactFrom = new ContactList();
contactFrom.AddContact("Tom", "");

// Compose the message object
Message message = messageComposer.ComposeMessage(contactFrom, null, contactTo, null, null, "Message subject");

// Send the message
if (message != null)

// Disconnect the session

Simple POP3 message retrieval

Let's assume we would like to retrieve only the first message from the server (if any):

// Create session object
Pop3Session pop3Session = new Pop3Session(60);

// Establish connection and authenticate
pop3Session.Authenticate("mylogin", "mypassword");

// Create message history list
MessageInfoList inMessageInfoList = new MessageInfoList("local_path\history");

// Calculate the amount of messages to be retrieved
UInt32 messageCount = inMessageInfoList.GetCount(

if (messageCount > 0)
// Create message info object
EmaiMessageInfo outInfo = new EmaiMessageInfo();
inMessageInfoList.GetInfo(currentMessageNumber, ref outInfo, (UInt32)EmaiEnumPop3MessageInfoListGetCountFlags.EmaiMessagesForDownload);

// Retrieve message
Message message = pop3Session.RetrieveMessage(outInfo.Number);

// Mark message as received at local message history

// Disconnect session

Simple message object parsing

Let's extract the main message parts (such as Sender, Recipients, Subject, Body, etc):

// Create message object
Message messageObject = new Message(ref message);

// Create message part enumerator object
MessagePartEnumerator messagePartEnumerator = new MessagePartEnumerator(messageObject);

// Retrieve and print message subject
System.Console.WriteLine("Subject: " + messagePartEnumerator.Subject);

// Retrieve and print message sender(s)
System.Console.WriteLine("From: " + messagePartEnumerator.From);

// Retrieve and print message recipient(s)
System.Console.WriteLine("To: " + messagePartEnumerator.To);

// Retrieve and print message plain text body
System.Console.WriteLine("Plain text body: " + messagePartEnumerator.Text);

// Retrieve and print message rich text body
System.Console.WriteLine("HTML text body: " + messagePartEnumerator.Html);

Now let's extract the message attachments:

// Create message part enumerator object
MessagePartEnumerator messagePartEnumerator = new MessagePartEnumerator(message);

// Retrieve and print message attachment(s) file name and size and content ID for inline attachments
Int32 attachmentsCount = messagePartEnumerator.AttachmentsCount;
if (attachmentsCount > 0)
for (Int32 j = 0; j < attachmentsCount; j++)
String fileName = "";
String contentID = "";
Byte[] data = new Byte[1];
messagePartEnumerator.GetAttachmentAtIndex(j, ref data, ref fileName, ref contentID);

System.Console.WriteLine("file name: " + fileName);
if (contentID.Length > 0)
System.Console.WriteLine("content ID: " + contentID);
System.Console.WriteLine("file size: " + data.Length);

And a simple IMAP message retrieval example

Let's assume we would like to print the subject of each message:

// Create session object
ImapSession imapSession = new ImapSession(60);
ImapResponseHandler imapResponseHandler = new ImapResponseHandler(ref imapSession);

// Establish connection and authenticate
imapSession.Connect("", (Int16)EmaiEnumDefaultImapPorts.EmaiDefaultImapPort);
imapSession.Authenticate("", "password", (UInt32)EmaiEnumImapAuthModes.EmaiIMAPAuthAutomatic);

// Enumerate IMAP folders and find the Inbox
imapSession.ListMailboxes("", "*");
ImapFolder folder = imapResponseHandler.FindFolderByName("INBOX");

if (folder != null)

// Retrieve the number of messages
UInt32 messageNumber = folder.Exists;
System.Console.WriteLine("Number of messages: " + messageNumber.ToString());

// Fetch all messages from the folder
EmaiFetchMessageAttribute[] fetchAttr = new EmaiFetchMessageAttribute[1];
fetchAttr[0] = new EmaiFetchMessageAttribute();
fetchAttr[0].Selector = (UInt32)EmaiEnumImapFetchOptions.EmaiFetchRFC822;

EmaiFetchMessageData fetchData = new EmaiFetchMessageData();
fetchData.Version = (UInt32)EmaiEnumStructureVersion.EmaiStructureVersionFirst;
fetchData.Selector = (UInt32)EmaiEnumImapDataFetchOptions.EmaiDataFetchAttr;
fetchData.param.attr.AttributeCount = (UInt32)fetchAttr.Length;
fetchData.param.attr.Attributes = fetchAttr;

for (UInt32 i = 1; i <= messageNumber; ++i)
ImapSequenceSet msgSequence = new ImapSequenceSet();

// Retrieve message and print the subject
imapSession.Fetch(ref msgSequence, ref fetchData);

Message message = folder.GetMessageDataAtIndex(i).Message;

MessagePartEnumerator messagePartEnumerator = new MessagePartEnumerator(message);

System.Console.WriteLine("Subject: " + messagePartEnumerator.Subject);

// Disconnect session
© 1999-2011 Dacons LLP | Term of Use | Online Privacy Policy | Contact Webmaster