473,802 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing the inbox through MAPI using C# .NET

17 New Member
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 31864
NitinSawant
270 Contributor
Can u pls tell me how to access rediffmail inbox using c#??
May 23 '08 #2
wheresjeannine
1 New Member
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 Recognized Expert Moderator MVP
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
2283
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. It is telling me that items is not a collection. I've tried everything I can think of, I've even copied the code from a few sources (had to add specific OOM in front of method names), Including Sues book, some code from www.outlookcode.com, and...
1
4594
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 the Internet Headers scrollbox. BUT -- when i run the Visual Basic code ( so far this is the only documented method I have found, so I'm converting it to c# ) found here: http://support.microsoft.com/?kbid=194870
3
6049
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 the error on the indicated line. Any help would be appreciated. Thanks Regards
2
5367
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 think of, I've even copied the code from a few sources (had to add specific OOM in front of method names), Including Sues book, some code from www.outlookcode.com, and one other place. but other than that, no change to copied code. I think the...
8
3192
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 thought it would be dead easy to migrate it to C#, however it is proving more difficult than I first thought. The VB code is as follows: Set oSession = CreateObject("MAPI.Session") oSession.Logon("Outlook", , False)
2
5917
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 = Olapp.GetNamespace("MAPI") Set Olfolder = Olmapi.GetDefaultFolder(6) '6 for olfolderinbox MsgBox Olfolder.name & " " & Olfolder.Items.Count
2
4601
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 MAPI.Session Dim objInbox As MAPI.Folder Dim objMessages As MAPI.Messages Dim objMessage As MAPI.Message
0
3464
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 original script that matters is: ' URL used for Outlook Web Access strOWAURL = "http://servername/exchange/"
1
1914
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 dont want Outlook MAPI to be used.... Thanks & Regards, Shweta
0
10535
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10303
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10282
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.