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

Mailit for C/C++ Windows Edition

The following code samples show how to send and receive messages

Mailit for C/C++ Windows Edition

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 C/C++ classes in your application. In this sample, we send a simple e-mail message with a few lines of code.

// Create session object
Emai::SmtpSession smtpSession(NULL, NULL, 60);

// Establish connection and authenticate
smtpSession.Connect(L"smtp.myisp.com");
smtpSession.Authenticate(L"mylogin", L"mypassword", EmaiSMTPAuthAutomatic);

// Create composer object and assign the text
MessageComposer messageComposer;
messageComposer.SetText("Message Text", EmaiUSASCII);

// Create contact list and assign a recipient
Emai::ContactList contactTo;
contactTo.AddContact(L"Joe", L"joe@somedomain.com");

// Create the sender address list
Emai::ContactList contactFrom;
contactFrom.AddContact(L"Tom", L"tom@mydomain.com");

// Compose the message object
Emai::Message message = messageComposer.ComposeMessage(contactFrom,
NULL, contactTo, NULL, NULL, L"Message subject");

// Send the message
if (message != NULL)
{
smtpSession.SendMessage(message);
}

// Disconnect the session
smtpSession.Disconnect();

Simple POP3 message retrieval

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

// Create session object
Emai::Pop3Session pop3Session(NULL, NULL, 60);

// Establish connection and authenticate
pop3Session.Connect(L"pop.mydomain.com", 110, EmaiNullOptions);
pop3Session.Authenticate(L"mylogin", L"mypassword");

// Create message history list
Emai::MessageInfoList inMessageInfoList(L"history");
pop3Session.RetrieveMessageList(inMessageInfoList);

// Calculate the amount of messages to be retrieved
EmaiUint32 messageCount = inMessageInfoList.GetCount();

if (messageCount > 0)
{
// Create message info object
EmaiMessageInfo outInfo;
inMessageInfoList.GetInfo(/*currentMessageNumber*/34, outInfo);

// Retrieve message
Emai::Message message = pop3Session.RetrieveMessage(outInfo.messageNumber);

// Mark message as received at local message history
inMessageInfoList.MarkMessageReceived(outInfo.messageNumber);
}

// Disconnect session
pop3Session.Disconnect();

Simple message object parsing

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

// Create message object
Emai::Message messageObject(message);

// Create message part enumerator object
MessagePartEnumerator messagePartEnumerator(messageObject);
messagePartEnumerator.Enumerate();

// Retrieve and print message subject
std::cout << "Subject: " << messagePartEnumerator.GetSubject().c_str() << std::endl;
std::cout << std::endl;

// Retrieve and print message sender(s)
std::cout << "From: ";
Emai::ContactList contacts = messagePartEnumerator.GetContacts(EmaiFrom);
EmaiUint32 count = contacts.GetCount();
for (EmaiUint32 i = 0; i < count; ++i)
std::cout << contacts.GetName(i) << "\" " << contacts.GetAddress(i) << std::endl;
std::cout << std::endl;

// Retrieve and print message recipient(s)
std::cout << "To: ";
contacts = messagePartEnumerator.GetContacts(EmaiTo);
count = contacts.GetCount();
for (EmaiUint32 i = 0; i < count; ++i)
std::cout << contacts.GetName(i) << "\" " << contacts.GetAddress(i) << std::endl;
std::cout << std::endl;

// Retrieve and print message plain text body
Emai::String text;
messagePartEnumerator.GetText(text);
std::cout << "Plain text body:" << std::endl << text;

// Retrieve and print message rich text body
messagePartEnumerator.GetHtml(text);
std::cout << "HTML text body:" << std::endl << text;

Now let's extract the message attachments:

// Retrieve and print message attachment(s) file name and size
EmaiUint32 attachmentsCount = messagePartEnumerator.GetAttachmentsCount();
EmaiUint32 length = 0;
if (attachmentsCount > 0)
{
std::cout << "Attachment:" << std::endl;
Emai::UniString fileName;
for (EmaiUint32 j = 0; j < attachmentsCount; j++)
{
messagePartEnumerator.GetAttachmentAtIndex(j, fileName, NULL, length);

std::cout << " file name: " << fileName.c_str() << std::endl;
std::cout << " file size: " << length << std::endl;
}
std::cout << std::endl;
}

// Retrieve and print message inline(s) file name, size and content id
EmaiUint32 inlinesCount = messagePartEnumerator.GetInlinesCount();
if (inlinesCount > 0)
{
std::cout << "Attachment inline:" << std::endl;
Emai::UniString fileName;
Emai::UniString contentID;
for (EmaiUint32 j = 0; j < inlinesCount; j++)
{
messagePartEnumerator.GetInlineAtIndex(j, fileName, contentID, NULL, length);

std::cout << " file name: " << fileName.c_str() << std::endl;
std::cout << " content ID: " << contentID.c_str() << std::endl;
std::cout << " file size: " << length << std::endl;
}
std::cout << std::endl;
}

And a simple IMAP message retrieval example

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

// Create Status callback and Responce handler objects
ImapCallback statusCallback;
ImapResponseHandler imapResponseHandler;

// Create session object
Emai::ImapSession imapSession(&imapResponseHandler, statusCallback, 60);

// Establish connection and authenticate
imapSession.Connect(L"mail.server.net");
imapSession.Authenticate(L"account.name", L"password", EmaiIMAPAuthAutomatic);

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

if (folder != NULL)
{
imapSession.SelectMailbox(L"inbox");

// Retrieve the number of
EmaiUint32 messageNumber = folder->GetExists();
std::cout << "Number of messages: " << messageNumber << std::endl;

// Fetch all messages from the folder
EmaiFetchMessageAttribute fetchAttr = {EmaiFetchRFC822};
EmaiFetchMessageData fetchData = {EmaiStructureVersionFirst};
fetchData.selector = EmaiDataFetchAttr;
fetchData.param.attr.attributeCount = 1;
fetchData.param.attr.attributes = &fetchAttr;

for (EmaiUint32 i = 1; i <= messageNumber; ++i)
{
//EmaiUniChar msgSequence[16];
Emai::ImapSequenceSet msgSequence;
msgSequence.AddNumber(i);

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

Emai::Message message = folder->GetMessageDataAtIndex(i)->GetMessage();

MessagePartEnumerator messagePartEnumerator(message);
messagePartEnumerator.Enumerate();

std::cout << "Subject:" << std::endl << " " << messagePartEnumerator.GetSubject().c_str() << std::endl << std::endl;
}
}

// Disconnect session
imapSession.Disconnect();
© 1999-2011 Dacons LLP | Term of Use | Online Privacy Policy | Contact Webmaster