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
-
//Declarations
-
MAPI.Message objMsg, objMsg_Arch;
-
MAPI.Messages objMsgs;
-
MAPI.Attachment objAttach;
-
MAPI.Attachments objAttachments;
-
MAPI.Folder objFolder;
-
MAPI.AddressEntry objAddressEntry;
-
int intTotalMsgs;
-
string strLocalDir;
-
-
//To get the total message count in the inbox
-
objFolder = (Folder)objSession.Inbox;
-
objMsgs = (Messages)objFolder.Messages;
-
intTotalMsgs = (int)objMsgs.Count;
-
-
//To get the sender
-
objMsg = (MAPI.Message)(objMsgs.get_Item(intMsgLoop));
-
objAddressEntry = (AddressEntry)objMsg.Sender;
-
-
//To save the attachment to local disk
-
objAttachments = (Attachments)objMsg.Attachments;
-
objAttach = (Attachment)objAttachments.get_Item(intMsg);
-
if ((CdoAttachmentType)objAttach.Type == CdoAttachmentType.CdoFileData)
-
{
-
objAttach.WriteToFile(strLocalDir + objAttach.Name.ToString());
-
}
-
-
//To create a folder in the inbox
-
Folder objFolder;
-
Folders objFolders;
-
Folder objItemFolder;
-
int intFolderCount;
-
string strFldrArch;
-
bool blnOK;
-
-
objFolder = (Folder)objSession.Inbox;
-
objFolder = (Folder)objSession.GetFolder(objFolder.ID,objFolder.StoreID);
-
objFolders = (Folders) objFolder.Folders;
-
intFolderCount = (int)objFolders.Count;
-
-
for (int intLoop = 1; intLoop <= intFolderCount; intLoop++)
-
{
-
objItemFolder = (Folder)objFolders.get_Item(intLoop);
-
if (objItemFolder.Name.ToString() == strFldrArch)
-
{
-
blnOK = true;
-
objFolder = objItemFolder;
-
break;
-
}
-
}
-
if (!blnOK)
-
{
-
objFolder = (Folder)objFolders.Add(strFldrArch);
-
objFolder.Update(Missing.Value, Missing.Value);
-
}
-
-
//To delete the message from the inbox
-
objMsg.Delete(Missing.Value);
-
-
//To forward the message
-
Message objMsgFwd;
-
Recipients objRecipients;
-
-
objMsgFwd = (MAPI.Message)objMsg.Forward();
-
objRecipients = (Recipients)objMsgFwd.Recipients;
-
objRecipients.Add(<RecipientAddr>, Missing.Value, CdoRecipientType.CdoTo,
-
Missing.Value);
-
objMsgFwd.Text = <Body Message>;
-
objMsgFwd.Recipients = objRecipients; objRecipients.Resolve(Missing.Value);
-
objMsgFwd.Send(false, Missing.Value, Missing.Value);
-
-
//To move the message to a folder
-
object objFolderID, objStoreID;
-
objFolderID = objFolder.ID; //objFolder is the folder where the msg to
-
be moved
-
objStoreID = objFolder.StoreID;
-
objMsgArch = (MAPI.Message)objMsg.MoveTo(objFolderID, objStor
-