How to use the Cdosys.dll library to process mail in the Drop directory by using Visual C#


This article describes how to use Microsoft Collaboration Data Objects (CDO) for Windows 2000 library (Cdosys.dll) to act on a message. This article describes how to act on a message in Microsoft Visual C# .NET or Microsoft Visual C# 2005. To act on a message, you can do any one of the following:
  • You can click Reply.
  • You can click Reply All.
  • You can click Forward.
Note The Cdosys.dll library is also known as CDOSYS.

More Information

To use CDOSYS to act on a message, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual C# 2005.
  2. On the File menu, click New, and then click Project.
  3. Click Console Application in the Visual C# Projects types list.

    Note In Microsoft Visual C# 2005, click Console Application in the Visual C# 2005 list.

    In Visual Studio .NET, Class1.cs is created by default. In Visual Studio 2005, Program.cs is created by default.
  4. Add a reference to the Microsoft CDO For Windows 2000 Library.

    To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Microsoft CDO For Windows 2000 Library, and then click Select.

      Note In Visual Studio 2005, you do not have to click Select.
    3. In the Add References dialog box, click OK to accept your selections.

      If you receive a message to generate wrappers for the libraries that you selected, click Yes.
  5. In the Code window, replace all the code with the following code:
    namespace CdoSys
    using System;
    class Class1
    static void Main(string[] args)
    CDO.DropDirectory iDropDir = new CDO.DropDirectory();
    CDO.IMessages iMsgs;

    CDO.IMessage iMsgReply;
    CDO.IMessage iMsgReplyAll;
    CDO.IMessage iMsgForward;

    // Get the messages from the Drop directory.
    iMsgs = iDropDir.GetMessages("C:\\Inetpub\\mailroot\\Drop");
    Console.WriteLine("Messages Count : " + iMsgs.Count.ToString());

    foreach (CDO.IMessage iMsg in iMsgs)

    // Output some common properties of the extracted message.
    Console.WriteLine("Subject: " + iMsg.Subject);
    Console.WriteLine("TextBody: " + iMsg.TextBody);
    Console.WriteLine("datereceived: " + iMsg.Fields["urn:schemas:httpmail:datereceived"].Value);
    Console.WriteLine("sendername: " + iMsg.Fields["urn:schemas:httpmail:sendername"].Value);
    Console.WriteLine("senderemail: " + iMsg.Fields["urn:schemas:httpmail:senderemail"].Value);
    Console.WriteLine("from: " + iMsg.Fields["urn:schemas:httpmail:from"].Value);
    Console.WriteLine("sender: " + iMsg.Fields["urn:schemas:httpmail:sender"].Value);

    // Reply.
    iMsgReply = iMsg.Reply();

    // TODO: Change "" to your e-mail address.
    iMsgReply.From = "";
    iMsgReply.TextBody = "I agree. You can continue." + "\n\n" + iMsgReply.TextBody;

    // This is ReplyAll.
    iMsgReplyAll = iMsg.ReplyAll();

    // TODO: Change "" to your e-mail address.
    iMsgReplyAll.From = "";
    iMsgReplyAll.TextBody = "I agree. You can continue" + "\n\n" + iMsgReplyAll.TextBody;

    // This is Forward.
    iMsgForward = iMsg.Forward();

    // TODO: Change "" to your e-mail address.
    iMsgForward.From = "";
    // TODO: Change "" to the address that you want to forward to.
    iMsgForward.To = "";
    iMsgForward.TextBody = "You missed this." + "\n\n" + iMsgForward.TextBody;

    // Clean up memory.
    iMsgs = null;
    iMsgReply = null;
    iMsgReplyAll = null;
    iMsgForward = null;
    catch (Exception e)
    Console.WriteLine("{0} Exception caught.", e);
  6. Where "TODO:" appears in the previous sample code, modify the code as indicated.
  7. Press F5 to build and to run the program.


For additional information about Microsoft Office development with Visual Studio, visit the following Microsoft Developer Network (MSDN) Web site:For additional information about how to use CDOSYS, click the following article numbers to view the articles in the Microsoft Knowledge Base:

310221 How to use the Cdosys.dll library to embed a message in a new message by using Visual C# .NET

310225 How to use the Cdosys.dll library to save a message to a file by using Visual C# .NET

310212 How to use the Cdosys.dll library to send an e-mail with attachments


ID do Artigo: 310224 - Última Revisão: 29 de abr de 2008 - Revisão: 1