zoukankan      html  css  js  c++  java
  • 如何使用 Cdosys.dll 库使用 Visual C# 中发送电子邮件带有附件接收邮件

    from: http://support.microsoft.com/kb/310221
    http://support.microsoft.com/kb/310224/
    http://support.microsoft.com/kb/310225/
    http://support.microsoft.com/kb/310212/

    如何使用 Cdosys.dll 库一封新邮件中嵌入一条消息,使用 Visual C#

    本文描述如何在新邮件中嵌入邮件的 Windows 2000 库 (Cdosys.dll) 中使用 Microsoft 协作数据对象 (CDO)。 可以使用 Microsoft Visual C# 执行此操作。

    请注意 Cdosys.dll 库也称为是 CDOSYS。
    若要用于新邮件中嵌入邮件 CDOSYS 请按照下列步骤操作:

       1. 启动 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005。
       2. 在 文件 菜单上单击 新建 ,然后单击 项目 。
       3. 单击 Visual C# 项目类型 列表中的 控制台应用程序 。

          情况默认,创建 Class 1.cs。

          请注意 在 Microsoft Visual C# 2005 必须在 Visual C# 列表中单击 控制台应用程序 。 情况默认,创建 Program.cs。
       4. 添加一个对 Microsoft CDO Windows 2000 库 的引用。 要这样做,请按下列步骤操作:
             1. 在 项目 菜单上单击 添加引用 。
             2. 在 COM 选项卡上找到 Microsoft CDO Windows 2000 库 ,然后单击 选择 。

                请注意 在 Microsoft Visual C# 2005 没有单击 选择 。
             3. 在 添加引用 对话框中,单击 确定 以接受您的选择。

                如果您收到一条消息在您选择的库生成包装,单击 是 。
       5. 在代码窗口中替换所有代码为以下代码:

          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;
                  }
                   }
          }

          oMsg.Send() ; iEmbdded = 空值 ; oMsg = 空值 ; iBp = 空值 ; iMsgs = 空值 ; iDropDir = null;} catch (Exception e) {Console.WriteLine ("捕获 {0} 异常",e);} 返回 ;}}}。
       6. 按 F 5 生成并运行该程序。

    如何使用 Cdosys.dll 库通过使用 Visual C# 中处理投递目录中的邮件

    本 文描述如何使用 for Windows 2000 库 (Cdosys.dll) 使其在邮件上的 Microsoft 协作数据对象 (CDO)。 本文介绍如何对 Microsoft Visual C#.NET 或 Microsoft Visual C# 2005 中的一个邮件操作。 若要操作消息,可以执行以下任何一个操作:

        * 您可以单击 答复 。
        * 您可以单击 全部答复 。
        * 您可以单击 。

    请注意 Cdosys.dll 库也称为是 CDOSYS。

    若要用于处理一条消息 CDOSYS 请按照下列步骤操作:

       1. 启动 Microsoft Visual Studio.NET 或 Microsoft Visual C# 2005 。
       2. 在 文件 菜单上单击 新建 ,然后单击 项目 。
       3. 单击 Visual C# 项目类型 列表中的 控制台应用程序 。

          请注意 Microsoft Visual C# 2005 中单击 Visual C# 2005 列表中的 控制台应用程序 。

          在 Visual 的 Studio.NET 中默认情况下将创建 Class 1.cs。 在 Visual 的 Studio 2005 中默认情况下被创建 Program.cs。
       4. 添加一个对 Microsoft CDO Windows 2000 库 的引用。

          要这样做,请按下列步骤操作:
             1. 在 项目 菜单上单击 添加引用 。
             2. 在 COM 选项卡上单击 Microsoft CDO Windows 2000 库 ,然后单击 选择 。

                请注意 在 Visual 的 Studio 2005 中没有单击 选择 。
             3. 在 添加引用 对话框中,单击 确定 以接受您的选择。

                如果您收到一条消息在所选库生成包装,单击 是 。
       5. 在代码窗口中替换所有代码为以下代码:

          namespace CdoSys
          {
              using System;
              class Class1
              {
                  static void Main(string[] args)
                  {
                      try
                      {          
                          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)
                          {
                              Console.WriteLine(iMsgs.get_FileName(iMsg));

                              // 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 "rhaddock@northwindtraders.com" to your e-mail address.
                              iMsgReply.From = "rhaddock@northwindtraders.com";
                              iMsgReply.TextBody = "I agree. You can continue." + " " + iMsgReply.TextBody;
                              iMsgReply.Send();

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

                              // TODO: Change "rhaddock@northwindtraders.com" to your e-mail address.
                              iMsgReplyAll.From = "rhaddock@northwindtraders.com";
                              iMsgReplyAll.TextBody = "I agree. You can continue" + " " + iMsgReplyAll.TextBody;
                              iMsgReplyAll.Send();

                              // This is Forward.
                              iMsgForward = iMsg.Forward();
                     
                              // TODO: Change "rhaddock@northwindtraders.com" to your e-mail address.
                              iMsgForward.From = "rhaddock@northwindtraders.com";
                              // TODO: Change "Jonathan@northwindtraders.com" to the address that you want to forward to.
                              iMsgForward.To = "Jonathan@northwindtraders.com";
                              iMsgForward.TextBody = "You missed this." + " " + iMsgForward.TextBody;
                              iMsgForward.Send();
                          }

                          // Clean up memory.
                          iMsgs = null;
                          iMsgReply = null;
                          iMsgReplyAll = null;
                          iMsgForward = null;
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine("{0} Exception caught.", e);
                      }
                      return;
                  }
              }
          }

          iMsgs = 空值 ; iMsgReply = 空值 ; iMsgReplyAll = 空值 ; iMsgForward = null;} catch (Exception e) {Console.WriteLine ("捕获 {0} 异常",e);} 返回;}}}。
       6. 其中"TODO:"出现在上面的示例代码修改代码如所示。
       7. 按 F 5 生成并运行该程序。


    如何使用 Cdosys.dll 库使用 Visual C# 将邮件保存到文件

    警 告  ADO 和 ADO MD 尚未进行完全测试 Microsoft.NET Framework 环境中。它们可能会间歇性的问题导致尤其是在基于服务的应用程序或多线程应用程序中。 本文中讨论的技术只应作为临时措施迁移到 ADO.NET 的过程中。必须进行完整的测试以确保任何兼容性问题后,只能使用这些方法。 由使用 ADO 或 ADO MD 中这种方式引起的任何问题是不受支持。有关详细信息,请参见在以下文章的 Microsoft 知识库文章:
    本文介绍如何使用协作数据对象 (CDO) 的 Windows 2000 库 (Cdosys.dll) 将邮件保存到文件在 Microsoft Visual C# 中。

    请注意 Cdosys.dll 库也称为是 CDOSYS

    "摘要"一节中描述用作 CDOSYS 请按照下列步骤操作:

       1. 启动 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005。
       2. 在 文件 菜单上单击 新建 ,然后单击 项目 。
       3. 项目类型 下, 单击 Visual C# 项目 ,然后单击 模板 下的 控制台应用程序 。 情况默认,创建 Class 1.cs。

          请注意 Microsoft Visual C# 2005 中单击 项目类型 下的 Visual C# 。 情况默认,创建 Program.cs。
       4. 添加一个对 Microsoft CDO Windows 2000 库 的引用。 要这样做,请按下列步骤操作:
             1. 在 项目 菜单上单击 添加引用 。
             2. 在 COM 选项卡上找到 Microsoft CDO Windows 2000 库 ,然后单击 选择 。

                请注意 在 Microsoft Visual C# 2005 没有单击 选择 。
             3. 若要接受您的选择,单击 添加引用 对话框中的 确定 。

                如果您收到一条消息在所选库生成包装,单击 是 。
       5. 在代码窗口中替换所有代码为以下代码:

          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;
                  }
                   }
          }

          stm.SaveToFile ("C:\temp\Test.eml、 ADODB.SaveOptionsEnum.adSaveCreateOverWrite) ; iMsg = 空值 ; iMsgs = 空值 ; iDropDir = 空值 ; iDsrc = 空值 ; STM = null;} catch (Exception e) {Console.WriteLine ("捕获 {0} 异常",e);} 返回 ;}}}。
       6. TODO 代码中出现的位置,修改代码,如所示。
       7. 要生成并运行该程序,按 F 5。


    如何使用 Cdosys.dll 库使用 Visual C# 中发送电子邮件带有附件

    本 文介绍如何使用协作数据对象 (CDO) 的 Windows 2000 库 (Cdosys.dll) 发送带附件的电子邮件。 可以发送文本或 HTML 或网页的电子邮件正文中通过使用本地的 SMTP 服务器或使用在 Microsoft Visual C# 中的智能主机服务器。

    请注意 Cdosys.dll 库也称为是 CDOSYS。

    "摘要"一节中描述用作 CDOSYS 请按照下列步骤操作:

       1. 启动 Microsoft Visual Studio。
       2. 在 文件 菜单上单击 新建 ,然后单击 项目 。
       3. 在 项目类型 ,下单击 Visual C# ,然后单击 模板 下的 控制台应用程序 。 情况默认,创建 Program.cs。

          请注意 在 Microsoft Visual C#.NET 2003 中将 Visual C# 更改为 Visual C# 项目 。 情况默认,创建 Class 1.cs。
       4. 添加一个对 Microsoft CDO Windows 2000 库 的引用。 要这样做,请按下列步骤操作:
             1. 在 项目 菜单上单击 添加引用 。
             2. 在 COM 选项卡上找到 Microsoft CDO Windows 2000 库 。

                请注意 在 Visual C# N 单击 选择 。
             3. 若要接受您的选择,单击 添加引用 对话框中的 确定 。

                如果您收到一个对话框,在所选库生成包装,单击 是 。
       5. 在代码窗口中替换所有代码为以下代码:

          namespace CdoSys
          {
              using System;
              class Class1
              {
                  static void Main(string[] args)
                  {
                      try
                      {           
                          CDO.Message oMsg = new CDO.Message();
                          CDO.IConfiguration iConfg;

                          iConfg = oMsg.Configuration;

                          ADODB.Fields oFields;
                          oFields = iConfg.Fields;      

                          // Set configuration.
                          ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
                                 
                          //TODO: To send by using the smart host, uncomment the following lines:
                          //oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
                          //oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
                          //oField.Value = "smarthost";

                          // TODO: To send by using local SMTP service.
                          //oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
                          //oField.Value = 1; 

                          oFields.Update();

                          // Set common properties from message.

                          //TODO: To send text body, uncomment the following line:
                          //oMsg.TextBody = "Hello, how are you doing?";
                     

                          //TODO: To send HTML body, uncomment the following lines:
                          //String sHtml;
                          //sHtml = " " +
                          //    " " +
                          //    "Sample GIF " +
                          //    "" +
                          //    "

    " +
                          //    "

    Inline graphics

    " +
                          //    "" +
                          //    "";
                          //oMsg.HTMLBody = sHtml;

                          //TOTO: To send WEb page in an e-mail, uncomment the following lines and make changes in TODO section.
                          //TODO: Replace with your preferred Web page
                          //oMsg.CreateMHTMLBody("http://www.microsoft.com",
                          //    CDO.CdoMHTMLFlags.cdoSuppressNone,
                          //    "", "");
                          oMsg.Subject = "Test SMTP"; 

                          //TODO: Change the To and From address to reflect your information.                      
                          oMsg.From = "someone@example.com";
                          oMsg.To = "someone@example.com";
                          //ADD attachment.
                          //TODO: Change the path to the file that you want to attach.
                          oMsg.AddAttachment("C:\Hello.txt", "", "");
                          oMsg.AddAttachment("C:\Test.doc", "", "");
                                              oMsg.Send();
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine("{0} Exception caught.", e);
                      }
                      return;
                  }
              }
          }

       6. TODO 代码中出现的位置,修改代码,如所示。
       7. 要生成并运行该程序,按 F 5。
       8. 请验证该电子邮件已被同时发送和收到。


  • 相关阅读:
    easy ui 常用控件配置
    oracel 查询语句
    .Net 取树形结构的数据
    Asp.Net Core File的操作
    js 页面技巧
    ASP.NET Core 文件上传
    ASP.NET EF实体主外键关系
    ASP.NET MVC 后台传值前端乱码解决方案 富文本Ueditor编辑
    手机访问网站自动跳转到手机版
    Ecshop布局参考图
  • 原文地址:https://www.cnblogs.com/geovindu/p/1594317.html
Copyright © 2011-2022 走看看