Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 2nd, 2008, 11:04 AM
Newbie
 
Join Date: Oct 2007
Posts: 17
Default Accessing the inbox through MAPI using C# .NET

This article discusses about how can we access the inbox through MAPI using C# .NET. It also discusses about how to store the attachment in the local directory, how to create a folder in the inbox

Need to add the reference Microsoft CDO 1.21 Library to the project

Expand|Select|Wrap|Line Numbers
  1. //Declarations
  2. MAPI.Message objMsg, objMsg_Arch;
  3. MAPI.Messages objMsgs;
  4. MAPI.Attachment objAttach;
  5. MAPI.Attachments objAttachments;
  6. MAPI.Folder objFolder;
  7. MAPI.AddressEntry objAddressEntry;
  8. int intTotalMsgs;
  9. string strLocalDir;
  10.  
  11. //To get the total message count in the inbox
  12. objFolder = (Folder)objSession.Inbox;
  13. objMsgs = (Messages)objFolder.Messages;
  14. intTotalMsgs = (int)objMsgs.Count;
  15.  
  16. //To get the sender
  17. objMsg = (MAPI.Message)(objMsgs.get_Item(intMsgLoop));
  18. objAddressEntry = (AddressEntry)objMsg.Sender;
  19.  
  20. //To save the attachment to local disk
  21. objAttachments = (Attachments)objMsg.Attachments;
  22. objAttach = (Attachment)objAttachments.get_Item(intMsg);
  23. if ((CdoAttachmentType)objAttach.Type == CdoAttachmentType.CdoFileData)
  24. {
  25.     objAttach.WriteToFile(strLocalDir + objAttach.Name.ToString());
  26. }
  27.  
  28. //To create a folder in the inbox
  29. Folder objFolder;
  30. Folders objFolders;
  31. Folder objItemFolder;
  32. int intFolderCount;
  33. string strFldrArch;
  34. bool blnOK;
  35.  
  36. objFolder = (Folder)objSession.Inbox;
  37. objFolder = (Folder)objSession.GetFolder(objFolder.ID,objFolder.StoreID);
  38. objFolders = (Folders) objFolder.Folders;
  39. intFolderCount = (int)objFolders.Count;
  40.  
  41. for (int intLoop = 1; intLoop <= intFolderCount; intLoop++)
  42. {
  43.    objItemFolder = (Folder)objFolders.get_Item(intLoop);
  44.    if (objItemFolder.Name.ToString() == strFldrArch)
  45.    {
  46.        blnOK = true;
  47.        objFolder = objItemFolder;
  48.        break;
  49.    }
  50. }
  51. if (!blnOK)
  52. {
  53.    objFolder = (Folder)objFolders.Add(strFldrArch);
  54.    objFolder.Update(Missing.Value, Missing.Value);
  55. }
  56.  
  57. //To delete the message from the inbox
  58. objMsg.Delete(Missing.Value);
  59.  
  60. //To forward the message
  61. Message objMsgFwd;
  62. Recipients objRecipients;
  63.  
  64. objMsgFwd = (MAPI.Message)objMsg.Forward();
  65. objRecipients = (Recipients)objMsgFwd.Recipients;
  66. objRecipients.Add(<RecipientAddr>, Missing.Value, CdoRecipientType.CdoTo, 
  67. Missing.Value);
  68. objMsgFwd.Text = <Body Message>;
  69. objMsgFwd.Recipients = objRecipients;                                                    objRecipients.Resolve(Missing.Value);
  70. objMsgFwd.Send(false, Missing.Value, Missing.Value);
  71.  
  72. //To move the message to a folder
  73. object objFolderID, objStoreID;
  74. objFolderID = objFolder.ID; //objFolder is the folder where the msg to 
  75. be moved
  76. objStoreID = objFolder.StoreID;
  77. objMsgArch = (MAPI.Message)objMsg.MoveTo(objFolderID, objStor
  78.  

Last edited by Plater; December 16th, 2008 at 06:50 PM. Reason: added code=c# tags
Reply



  #2  
Old May 23rd, 2008, 10:34 AM
Nitin646's Avatar
Member
 
Join Date: Oct 2007
Location: मुंबई, India
Age: 20
Posts: 109
Default

Can u pls tell me how to access rediffmail inbox using c#??
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.