473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

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.  
May 2 '08 #1
3 31771
NitinSawant
270 100+
Can u pls tell me how to access rediffmail inbox using c#??
May 23 '08 #2
Is this missing article some essential setup details because after adding the CDO as a reference this code does not even compile let alone run
Jun 8 '12 #3
Rabbit
12,516 Expert Mod 8TB
As far as I can tell, that code isn't meant to be fully functional in of itself. It is merely code snippets to be cut, pasted, and edited to fit your needs. The code in between each commented line is an example of how to accomplish the task of the preceding comment. For example,
Expand|Select|Wrap|Line Numbers
  1. //To delete the message from the inbox 
  2. objMsg.Delete(Missing.Value); 
That is just an example of how to delete a message. It doesn't actually delete anything unless you set objMsg to an actual message and replace Missing.Value with the correct boolean value.
Jun 8 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jason Ferree | last post by:
I posetd this in one group, but got no response, so I'll try it in this one. I am trying to loop through all messages in the inbox. To do something, in my case, look for and save attachments....
1
by: john bailo | last post by:
I am trying to write a c# program to access Outlook internet headers for messages in a public folder. When I look at these messages in Outlook, and select View/Options, there is information in...
3
by: John | last post by:
Hi I am trying to access outlook contacts folders and delete the contacts that do not contain a certain category value in the categories field. I have written the below code but am stuck with...
2
by: Jason Ferree | last post by:
I am trying to loop through all messages in the inbox. To do something, in my case, look for and save attachments. It is telling me that items is not a collection. I've tried everything I can...
8
by: Steve Le Monnier | last post by:
Can anybody help with the following: I have some old VB6 code that loops through my inbox looking for emails with attachments and then save them to the hard drive. The code is very simplistic so I...
2
by: salad | last post by:
I have the following code. Dim Olapp As Object Dim Olmapi As Object Dim Olfolder As Object Set Olapp = GetObject(, "Outlook.Application") 'get reference Set Olmapi =...
2
by: bill | last post by:
I have this code to get to the default Outlook inbox but I would like to get to the inbox of an additional added mailbox. Can this be done? Here is the code I'm using below: Dim objSession As...
0
by: davidiwharper | last post by:
Hello all I am trying to modify a CDO script that moves email messages out of a user's inbox and into a drop folder. I want to update it to use a Public Folder instead. The portion of the...
1
by: ShwetaJain | last post by:
Hi... I am able to get the type of the message as IPM.Appointmnet in the inbox but i am unable to get the due date, start date, for that appointment.... I am using MAPI in exchange server but i...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.