Microsoft Knowledge Base Email Alertz

(310225) - Describes how to use CDO for Windows 2000 library to save a message to a file. Contains a step-by-step Visual C# .NET sample.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 310225 - Last Review: December 3, 2007 - Revision: 4.2

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

This article was previously published under Q310225
Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:
840667   (http://kbalertz.com/Feedback.aspx?kbNumber=840667/ ) You receive unexpected errors when using ADO and ADO MD in a .NET Framework application

SUMMARY

This article describes how to use the Collaboration Data Objects (CDO) for Windows 2000 library (Cdosys.dll) to save a message to a file in Microsoft Visual C#.

Note The Cdosys.dll library is also known as CDOSYS.

MORE INFORMATION

To use CDOSYS as described in the "Summary" section, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, click New, and then click Project.
  3. Under Project Types, click Visual C# Projects, and then click Console Application under Templates. By default, Class1.cs is created.

    Note In Microsoft Visual C# 2005, click Visual C# under Project Types. By default, Program.cs is created.
  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, locate Microsoft CDO For Windows 2000 Library, and then click Select.

      Note In Microsoft Visual C# 2005, you do not have to click Select.
    3. To accept your selections, click OK in the Add References dialog box.

      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)
    		{
    			try 
    			{			
    			CDO.DropDirectory iDropDir = new CDO.DropDirectory();
    			CDO.IMessages iMsgs;
    			CDO.IMessage iMsg;
    			
    
    			iMsgs = iDropDir.GetMessages("C:\\Inetpub\\mailroot\\Drop");
    			Console.WriteLine("Count: " + iMsgs.Count);
    
    			// Get first message.
    			iMsg = iMsgs[1];
    
    			ADODB.Stream stm = new ADODB.Stream();
    			stm.Open(System.Reflection.Missing.Value, ADODB.ConnectModeEnum.adModeUnknown,
    				     ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified,
    				     "", "");
    
    			stm.Type = ADODB.StreamTypeEnum.adTypeText;
    			stm.Charset = "US-ASCII";
    
    			CDO.IDataSource iDsrc;
    			iDsrc = iMsg.DataSource;
    
    			iDsrc.SaveToObject(stm, "_Stream");
                               // TODO: modify the file path.
    			stm.SaveToFile("C:\\temp\\Test.eml", ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
    
    
    			iMsg = null;
    			iMsgs = null;
    			iDropDir = null;
    			iDsrc = null;
    			stm = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}
    			return;
    		}
             }
    }
  6. Where TODO appears in the code, modify the code as indicated.
  7. To build and run the program, press F5.

REFERENCES

For more information about Microsoft Office Development with Visual Studio, see the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
For additional information about how to use CDOSYS, click the following article numbers to view the articles in the Microsoft Knowledge Base:
310212  (http://kbalertz.com/Feedback.aspx?kbNumber=310212/ ) How to use the Cdosys.dll library to send an e-mail with attachments
310221  (http://kbalertz.com/Feedback.aspx?kbNumber=310221/ ) How to use the Cdosys.dll library to embed a message in a new message by using Visual C# .NET
310224  (http://kbalertz.com/Feedback.aspx?kbNumber=310224/ ) How to use the Cdosys.dll library to process mail in the Drop directory by using Visual C# .NET

APPLIES TO
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Collaboration Data Objects 2.0
  • Microsoft ActiveX Data Objects 2.5
  • Microsoft ActiveX Data Objects 2.6
  • Microsoft ActiveX Data Objects 2.7
  • Microsoft Internet Information Services 6.0
  • Microsoft Internet Information Services 5.0
Keywords: 
kbcode kbhowto KB310225
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

saqib mehmood - saqibcode NOSPAM-AT-NOSPAM hotmail.com Report As Irrelevant  
Written: 5/25/2005 10:49 PM
I get the exception while sending email using CDO.Message as The "SendUsing" configuration value is invalid

Jayant Report As Irrelevant  
Written: 9/2/2005 12:51 AM
How can I save a MailMessage object? I can create a MailMessage object and schedule it to send at some time. I want to save it. How is it possible.