static void Main(string[] args) { if (NetMAPI.Init()) { NetMAPI mapi = new NetMAPI(); if (mapi.Login()) { StringBuilder strText = new StringBuilder(NetMAPI.DefaultBufferSize); if (mapi.GetProfileName(strText)) Console.WriteLine("Profile Name: " + strText); if (mapi.GetProfileEmail(strText)) Console.WriteLine("Profile Email: " + strText); if (mapi.OpenMessageStore()) { // uncomment the functions you're interested in and/or step through these to see how each thing works. // SendTest(mapi); // SendCIDTest(mapi); // FolderTest(mapi); ReceiveTest(mapi); // ContactsTest(mapi); // CopyMessageTest(mapi); // MoveMessageTest(mapi); // DeleteMessageTest(mapi); // AppointmentTest(mapi); } mapi.Logout(); } NetMAPI.Term(); } Console.ReadLine(); }
/// <summary> /// Gets the rich text field of the item /// </summary> /// <param name="strRTF">rich text content</param> /// <returns>true on success</returns> public bool GetRTF(ref string strRTF) { IntPtr szString; if (!ObjectGetRTF(pObject, out szString)) { szString = IntPtr.Zero; } strRTF = NetMAPI.MarshalString(szString); return(szString != IntPtr.Zero); }
/// <summary> /// Gets the ANSI text field of the item, or optionally the "suitable" text /// </summary> /// <param name="strBody">text content</param> /// <param name="bAutoDetect">use the MessageEditorFormat to determine which field to query</param> /// <returns>true on success</returns> public bool GetBody(out string strBody, bool bAutoDetect) { IntPtr szString; if (!ObjectGetBody(pObject, out szString, bAutoDetect)) { szString = IntPtr.Zero; } strBody = NetMAPI.MarshalString(szString); return(szString != IntPtr.Zero); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // To iterate through folders: // -first open up a MAPI session and login // -then open the message store you want to access // -then open the folder and get the hierarchy table // -iterate through the folders using GetNextFolder() // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void FolderTest(NetMAPI mapi) { // another way to do this is with the line below // if (mapi.OpenRootFolder()) EnumerateSubFolders(mapi.Folder); if (mapi.OpenRootFolder() && mapi.GetHierarchy()) { StringBuilder s = new StringBuilder(NetMAPI.DefaultBufferSize); MAPIFolder folder; while (mapi.GetNextSubFolder(out folder, s)) { Console.WriteLine("Folder: " + s.ToString()); EnumerateSubFolders(folder); folder.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(); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // 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(); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // To embed images inside of HTML text: // -send a message as usual, setting its HTML text // -add an <IMG tag with src="cid:<contentID>" // -add an attachment using the same CID // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void SendCIDTest(NetMAPI mapi) { if (mapi.OpenOutbox()) { MAPIMessage message = new MAPIMessage(); if (message.Create(mapi, MAPIMessage.Importance.IMPORTANCE_LOW)) { message.SetSender("Support", "*****@*****.**"); message.SetSubject("Subject"); string strCID = "123456789"; StringBuilder strImage = new StringBuilder(); strImage.Append("<html><body><IMG alt=\"\" src=\"cid:"); strImage.Append(strCID); strImage.Append("\" border=0></body></html>"); message.SetRTF(strImage.ToString()); message.AddRecipient("*****@*****.**"); message.AddAttachment(@"c:\Temp\Pic.jpg", "", strCID); // obviously you'll have to supply an image here if (message.Send()) Console.WriteLine("Sent Successfully"); } } }
/// <summary> /// Create a new contact in the current folder /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <param name="pFolder">folder to create in</param> /// <returns>true on success</returns> public bool Create(NetMAPI mapi, IntPtr pFolder) { return(ContactCreate(mapi.MAPI, out pObject, pFolder)); }
/// <summary> /// Create a new contact in the current folder /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <returns>true on success</returns> public bool Create(NetMAPI mapi) { return ContactCreate(mapi.MAPI, out pObject, IntPtr.Zero); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // This function reads an appointment from the Calendar // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void AppointmentTest(NetMAPI mapi) { if (mapi.OpenCalendar() && mapi.GetContents()) { mapi.SortContents(false, SortFields.SORT_RECEIVED_TIME); MAPIAppointment appointment; StringBuilder strText = new StringBuilder(NetMAPI.DefaultBufferSize); if (mapi.GetNextAppointment(out appointment)) { if (appointment.GetSubject(strText)) Console.WriteLine("Subject: " + strText); if (appointment.GetLocation(strText)) Console.WriteLine("Location: " + strText); if (appointment.GetStartTime(strText)) Console.WriteLine("Start Time: " + strText); if (appointment.GetEndTime(strText)) Console.WriteLine("End Time: " + strText); appointment.Dispose(); } } }
/// <summary> /// Create a new message in the current folder /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <param name="nImportance">priority of the message</param> /// <param name="bSaveToSentFolder">save in sent or delete after sending</param> /// <param name="pFolder">folder to create in</param> /// <returns>true on success</returns> public bool Create(NetMAPI mapi, Importance nImportance, bool bSaveToSentFolder, IntPtr pFolder) { return(MessageCreate(mapi.MAPI, out pObject, (int)nImportance, bSaveToSentFolder, pFolder)); }
/// <summary> /// Create a new message in the current folder /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <param name="nImportance">priority of the message</param> /// <returns>true on success</returns> public bool Create(NetMAPI mapi, Importance nImportance) { return(MessageCreate(mapi.MAPI, out pObject, (int)nImportance, true, IntPtr.Zero)); }
/// <summary> /// Shows the default IMessage form for this message /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <returns>0 on failure, IDOK (1) on close or send and IDCANCEL (2) on close for new messages</returns> public int ShowForm(NetMAPI mapi) { return MessageShowForm(mapi.MAPI, pObject); }
/// <summary> /// Create a new message in the current folder /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <param name="nImportance">priority of the message</param> /// <param name="bSaveToSentFolder">save in sent or delete after sending</param> /// <param name="pFolder">folder to create in</param> /// <returns>true on success</returns> public bool Create(NetMAPI mapi, Importance nImportance, bool bSaveToSentFolder, IntPtr pFolder) { return MessageCreate(mapi.MAPI, out pObject, (int)nImportance, bSaveToSentFolder, pFolder); }
/// <summary> /// Create a new message in the current folder /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <param name="nImportance">priority of the message</param> /// <returns>true on success</returns> public bool Create(NetMAPI mapi, Importance nImportance) { return MessageCreate(mapi.MAPI, out pObject, (int)nImportance, true, IntPtr.Zero); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // 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(); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // 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(); } } }
/// <summary> /// Shows the default IMessage form for this message /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <returns>0 on failure, IDOK (1) on close or send and IDCANCEL (2) on close for new messages</returns> public int ShowForm(NetMAPI mapi) { return(MessageShowForm(mapi.MAPI, pObject)); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // This function displays your first contact in your Contacts folder // // To do this: // -first open up a MAPI session and login // -then open the message store you want to send // -open your contacts folder // // Use GetName to get the name (default DISPLAY NAME, but can be Initials, First Name etc) // Use GetEmail to get the email address (named property) // Use GetAddress to get the mailing address of CContactAddress::AddressType // Use GetPhoneNumber supplying a phone number property (ie BUSINESS_TELEPHONE_NUMBER) // Use GetNotes to get the notes in either plain text (default) or RTF // // Remember to Dispose of the contact when you're done with it! // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void ContactsTest(NetMAPI mapi) { if (mapi.OpenContacts() && mapi.GetContents()) { // sort by name (stored in PR_SUBJECT) mapi.SortContents(true, SortFields.SORT_SUBJECT); MAPIContact contact; StringBuilder strText = new StringBuilder(NetMAPI.DefaultBufferSize); if (mapi.GetNextContact(out contact)) { if (contact.GetName(strText, MAPIContact.NameType.DISPLAY_NAME)) Console.WriteLine("Contact: " + strText); if (contact.GetEmail(strText)) Console.WriteLine("Email: " + strText); if (contact.GetCategories(strText)) Console.WriteLine("Categories: " + strText); MAPIContact.ContactAddress address; if (contact.GetAddress(out address, MAPIContact.AddressType.BUSINESS)) { Console.WriteLine(address.Street); Console.WriteLine(address.City); Console.WriteLine(address.StateOrProvince); Console.WriteLine(address.Country); Console.WriteLine(address.PostalCode); } if (contact.GetPhoneNumber(strText, MAPIContact.PhoneType.BUSINESS_TELEPHONE_NUMBER)) Console.WriteLine("Phone: " + strText); //usually you would call GetNotesSize first to ensure the buffer is large enough if (contact.GetNotes(strText, false)) Console.WriteLine("Notes: " + strText); contact.Dispose(); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////// // // To send a message: // -first open up a MAPI session and login // -then open the message store you want to access // -then open the outbox // -create a new message, set its priority if you like // -set its properties, recipients and attachments // -call send // //////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void SendTest(NetMAPI mapi) { if (mapi.OpenOutbox()) { MAPIMessage message = new MAPIMessage(); if (message.Create(mapi, MAPIMessage.Importance.IMPORTANCE_LOW)) { message.SetSender("Support", "*****@*****.**"); message.SetSubject("Subject"); // user SetBody for ANSI text, SetRTF for HTML and Rich Text message.SetRTF("<html><body><font size=2 color=red face=Arial><span style='font-size:10.0pt;font-family:Arial;color:red'>Body</font></body></html>"); message.AddRecipient("*****@*****.**"); message.AddRecipient("*****@*****.**"); if (message.Send()) Console.WriteLine("Sent Successfully"); } } }
/// <summary> /// Create a new contact in the current folder /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <param name="pFolder">folder to create in</param> /// <returns>true on success</returns> public bool Create(NetMAPI mapi, IntPtr pFolder) { return ContactCreate(mapi.MAPI, out pObject, pFolder); }
/// <summary> /// Create a new contact in the current folder /// </summary> /// <param name="mapi">NetMAPI session with an open folder</param> /// <returns>true on success</returns> public bool Create(NetMAPI mapi) { return(ContactCreate(mapi.MAPI, out pObject, IntPtr.Zero)); }