Microsoft Knowledge Base Email Alertz

(310221) - Describes how to use the CDO for Windows 2000 library to embed a message in a new message. This article provides a step-by-step example that shows how to do this by using Visual C# .NET.

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: 310221 - Last Review: December 3, 2007 - Revision: 3.6

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

This article was previously published under Q310221

INTRODUCTION

This article describes how to use the Microsoft Collaboration Data Objects (CDO) for Windows 2000 library (Cdosys.dll) to embed a message in a new message. You can do this by using Microsoft Visual C#.

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

MORE INFORMATION

To use CDOSYS to embed a message in a new message, 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. Click Console Application in the Visual C# Projects types list.

    By default, Class1.cs is created.

    Note In Microsoft Visual C# 2005, you must click Console Application in the Visual C# list. 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. 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 select, 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.IBodyPart iBp;
    			CDO.IMessage iEmbdded;
    			CDO.Message oMsg;
    			CDO.IDataSource iDsrc;
    
    			// Create a new message.
    			oMsg = new CDO.Message();
    			oMsg.From = "rhaddock@northwindtraders.com";
    			oMsg.To = "rhaddock@northwindtraders.com";
    			oMsg.Subject = "CDOSYS Send mail";
    			oMsg.TextBody = "Hello";
    			iBp = oMsg.BodyPart.AddBodyPart(-1);
    
    			// Embed the message.
    			iMsgs = iDropDir.GetMessages("C:\\Inetpub\\mailroot\\Drop");
    			Console.WriteLine("Messages Count : " + iMsgs.Count.ToString());
    
    			// Get first message.
    			iEmbdded = iMsgs[1];
    
    			// Embed the message in oMsg by using BodyPart.
    			iDsrc = iEmbdded.DataSource;
    			iDsrc.SaveToObject(iBp, "IBodyPart");
    
    
    			// Send mail.
    			oMsg.Send();
    
    			iEmbdded = null;
    			oMsg = null;
    			iBp = null;
    			iMsgs = null;
    			iDropDir = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}
    			return;
    		}
             }
    }
  6. Press F5 to build and to run the program.

REFERENCES

For additional information about Microsoft Office development with Visual Studio, visit 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:
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
310225  (http://kbalertz.com/Feedback.aspx?kbNumber=310225/ ) How to use the Cdosys.dll library to save a message to a file by using Visual C# .NET
310212  (http://kbalertz.com/Feedback.aspx?kbNumber=310212/ ) How to use the Cdosys.dll library to send an e-mail with attachments

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 5.0
  • Microsoft Internet Information Services 6.0
Keywords: 
kbcode kbhowto KB310221
       

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