Dacons LLP Mailit for C/C++ Version 1.0.8

Using Mailit for C/C++ in Your Projects

    

The following steps demonstrate how to use Mail Library in your applications.

Referencing Mail Library Objects

In Visual Studio .NET

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("smtp.myisp.com");
 smtpSession.Authenticate("mylogin", "mypassword", EmaiSMTPAuthAutomatic);
 
 // Create composer object and assign the text
 MessageComposer messageComposer;
 messageComposer.SetText("Message Text", kEmaiUSASCII);

 // 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, 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() << 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 << "\tfile name: " << fileName << std::endl;
		std::cout << "\tfile 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::String contentID;
	for (EmaiUint32 j = 0; j < inlinesCount; j++)
	{
		messagePartEnumerator.GetInlineAtIndex(j, fileName, contentID, NULL, length);
		
		std::cout << "\tfile name: " << fileName << std::endl;
		std::cout << "\tcontent ID: " << contentID << std::endl;
		std::cout << "\tfile 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.ListFolders(L"", L"");
 ImapFolder* folder = imapResponseHandler.FindFolderByName(L"inbox");

 if (folder != NULL)
 {
   imapResponseHandler.SetActiveFolder(folder);
   imapSession.SelectFolder(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];
     ::wnsprintf(msgSequence, sizeof(msgSequence), _T("%d"), inMessageNumber);
 
	// 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 << "\t" << messagePartEnumerator.GetSubject() << std::endl << std::endl;
   }
 }

 // Disconnect session
 imapSession.Disconnect();
 
     Assigning the license key