//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // This function deletes the first message from the Inbox // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void DeleteMessageTest(NetMAPI mapi) { if (mapi.OpenInbox() && mapi.GetContents()) { mapi.SortContents(false); MAPIMessage message; StringBuilder s = new StringBuilder(NetMAPI.DefaultBufferSize); if (mapi.GetNextMessage(out message)) { Console.Write("Deleting message from '"); message.GetSenderName(s); Console.Write(s.ToString() + "'\n"); mapi.Folder.DeleteMessage(message); message.Dispose(); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // This function creates a folder (opens if exists) and copies the first unread message if any to this folder // // To do this: // -first open up a MAPI session and login // -then open the message store you want to access // -then open the folder (probably inbox) and get the contents table // -open the message you want to move // -create (open if exists) the folder you want to move to // -copy the message // // You can also move and delete the message, but I wanted the sample to be non destructive just in case // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void CopyMessageTest(NetMAPI mapi) { if (mapi.OpenInbox() && mapi.GetContents()) { mapi.SetUnreadOnly(true); MAPIMessage message; StringBuilder s = new StringBuilder(NetMAPI.DefaultBufferSize); if (mapi.GetNextMessage(out message)) { Console.Write("Copying message from '"); message.GetSenderName(s); Console.Write(s.ToString() + "' ("); message.GetSenderEmail(s); Console.Write(s.ToString() + "), subject '"); message.GetSubject(s); Console.Write(s.ToString() + "'\n"); MAPIFolder folder=mapi.Folder; MAPIFolder subfolder; if (folder.CreateSubFolder("TestFolder", out subfolder)) { if (folder.CopyMessage(message, subfolder)) { Console.WriteLine("Message copied successfully"); } subfolder.Dispose(); } message.Dispose(); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // This function moves the first message from the Inbox to the Sent Items folder (requested by user) // // NOTE: MAPIEx takes care of the internal folder, but in this case you have an external folder that must // be disposed of. // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void MoveMessageTest(NetMAPI mapi) { if (mapi.OpenInbox() && mapi.GetContents()) { mapi.SortContents(false); MAPIMessage message; StringBuilder s = new StringBuilder(NetMAPI.DefaultBufferSize); if (mapi.GetNextMessage(out message)) { Console.Write("Moving message from '"); message.GetSenderName(s); Console.Write(s.ToString() + "' ("); message.GetSenderEmail(s); Console.Write(s.ToString() + "), subject '"); message.GetSubject(s); Console.Write(s.ToString() + "'\n"); MAPIFolder sentItems = mapi.OpenSentItems(false); if (mapi.Folder.MoveMessage(message, sentItems)) { Console.WriteLine("Message moved successfully"); } sentItems.Dispose(); message.Dispose(); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // To receive a message: // -first open up a MAPI session and login // -then open the message store you want to access // -then open the inbox and get the contents table // -iterate through the message using GetNextMessage() (sample below gets only unread messages) // -save attachments (if any) using SaveAttachment() if you like // // Remember to Dispose of the message when you're done with it! // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void ReceiveTest(NetMAPI mapi) { if (mapi.OpenInbox() && mapi.GetContents()) { mapi.SortContents(false); mapi.SetUnreadOnly(false); MAPIMessage message; StringBuilder s = new StringBuilder(NetMAPI.DefaultBufferSize); while (mapi.GetNextMessage(out message)) { Console.Write("Message from '"); message.GetSenderName(s); Console.Write(s.ToString() + "' ("); message.GetSenderEmail(s); Console.Write(s.ToString() + "), subject '"); message.GetSubject(s); Console.Write(s.ToString() + "', received: "); message.GetReceivedTime(s); Console.Write(s.ToString() + "\n\n"); // use message.GetBody(), message.GetHTML(), or message.GetRTF() to get the text body // GetBody() can autodetect the source string strBody; message.GetBody(out strBody, true); Console.Write(strBody + "\n"); message.Dispose(); } } }