文章编号: 310263 - 最后修改: 2007年6月29日 - 修订: 4.7

如何使用 Microsoft Outlook 对象库发送一条消息,通过使用 Visual C# 有附件

系统提示此文章适用于与您所使用的操作系统不同的操作系统。文章内容可能与您无关,并且已被禁用。
展开全部 | 关闭全部

概要

本文介绍如何使用 Microsoft Outlook 2002 对象库或在 Microsoft Office Outlook 2003 对象库发送了通过使用 Microsoft Visual C# 包含附件的邮件 (也就是一个 MailItem 对象)。

更多信息

若要发送包含附件的邮件使用 Microsoft Outlook 对象库,请按照下列步骤操作:
  1. 启动 Microsoft Visual Studio.net 或 Microsoft Visual Studio 2005。
  2. 文件 菜单上指向 新建,然后单击 项目
  3. 项目类型 框中,单击 Visual C# 项目

    注意在 Visual 有关 Studio 的 2005 年单击 Visual C#项目类型 下。
  4. 模板 框中,单击 控制台应用程序

    默认状态下,创建 Class1.cs。


    注意在 Microsoft Visual Studio 2005 年 Program.cs 是默认情况下创建的。
  5. 将引用添加到 Microsoft Outlook 2002 对象库或在 Microsoft Office Outlook 2003 对象库中。这样做,请按照下列步骤操作:
    1. 项目 菜单上单击 添加引用
    2. 单击 COM 选项卡,单击所需在 Microsoft Outlook 对象库,然后单击 选择

      注意在 Visual 有关 Studio 的 2005 年没有单击 选择
    3. 添加引用 对话框中单击 确定
    4. 如果系统提示您所选的库生成包装,单击
  6. 到 Class1.cs 代码窗口中,用下列替换代码:
    using System;
    //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.
    //using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace Outlook_SendMailItem
    {
    	public class Class1
    	{
    		public static int Main(string[]args)
    		{
    			try
    			{
    				// Create the Outlook application by using inline initialization.
    				Outlook.Application oApp = new Outlook.Application();
    
    				//Create the new message by using the simplest approach.
    				Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    
    				//Add a recipient.
    				// TODO: Change the following recipient where appropriate.
    				Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add("e-mail address");
    				oRecip.Resolve();
    
    				//Set the basic properties.
    				oMsg.Subject = "This is the subject of the test message";
    				oMsg.Body = "This is the text in the message.";
    
    				//Add an attachment.
    				// TODO: change file path where appropriate
    				String sSource = "C:\\setupxlg.txt";
    				String sDisplayName = "MyFirstAttachment";
    				int iPosition = (int)oMsg.Body.Length + 1;
    				int iAttachType = (int)Outlook.OlAttachmentType.olByValue;  
    				Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource,iAttachType,iPosition,sDisplayName);
    
    				// If you want to, display the message.
    				// oMsg.Display(true);  //modal
    
    				//Send the message.
    				oMsg.Save();
    				oMsg.Send();
    
    				//Explicitly release objects.
    				oRecip = null;
    				oAttach = null;
    				oMsg = null;
    				oApp = null;
    			}
    
    				// Simple error handler.
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught: ", e);
    			}
    
    			//Default return value.
    			return 0;
    
    		}
    
    	}
    }
    
  7. 在该的代码中搜索 TODO,然后修改为您的环境的代码。
  8. 按 F5 生成并运行程序。
  9. 请确保已发送和接收电子邮件。
注意 在 Outlook 中的安全功能将导致发生之前将邮件发送的一个或多个提示。 有关详细的信息,请参阅"参考"部分。

参考

有关详细的信息,请访问下面的 MSDN 网站:
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
有关详细的信息请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
290500? (http://support.microsoft.com/kb/290500/ ) 开发人员相关的电子邮件安全功能,在 Outlook 2002 中的说明

这篇文章中的信息适用于:
  • Microsoft Visual C# .NET 2003 标准版
  • Microsoft Visual C# .NET 2002 标准版
  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002 标准版
  • Microsoft Visual C# 2005
关键字:?
kbmt kbcode kbinterop kboutlookobj kbhowto KB310263 KbMtzh
机器翻译机器翻译
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。
点击这里察看该文章的英文版: 310263? (http://support.microsoft.com/kb/310263/en-us/ )
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。