通过 C# 使用 CDOSYS 发送已生成的 MHTML?

发布于 2024-09-05 09:36:53 字数 1425 浏览 5 评论 0原文

我有一个已生成的 MHTML 作为字节数组(来自 Aspose.Words),并且想将其作为电子邮件发送。我正在尝试通过 CDOSYS 来做到这一点,但我对其他建议持开放态度。目前,我有以下内容:

        CDO.Message oMsg = new CDO.Message();
        CDO.IConfiguration iConfg = oMsg.Configuration;
        Fields oFields = iConfg.Fields;

        // Set configuration.
        Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
        oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
        oField.Value = SmtpClient.Host;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
        oField.Value = SmtpClient.Port;
        oFields.Update();

        //oMsg.CreateMHTMLBody("http://www.microsoft.com", CDO.CdoMHTMLFlags.cdoSuppressNone,  "", "");
        // NEED MAGIC HERE :)
        oMsg.Subject = warning.Subject;             // string

        oMsg.From = "[email protected]";
        oMsg.To = warning.EmailAddress;
        oMsg.Send();

在此代码片段中,警告变量有一个 Body 属性,它是一个 byte[]。上面代码中写着“NEED MAGIC HERE”的地方我想使用这个 byte[] 来设置 CDO 消息的正文。

我已经尝试了以下方法,不出所料,这不起作用:

oMsg.HTMLBody = System.Text.Encoding.ASCII.GetString(warning.Body);

有人知道如何使用 CDOSYS 或其他东西实现我想要的东西吗?

I have a ready generated MHTML as a byte array (from Aspose.Words) and would like to send it as an email. I'm trying to do this through CDOSYS, though am open to other suggestions. For now though I have the following:

        CDO.Message oMsg = new CDO.Message();
        CDO.IConfiguration iConfg = oMsg.Configuration;
        Fields oFields = iConfg.Fields;

        // Set configuration.
        Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
        oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
        oField.Value = SmtpClient.Host;
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
        oField.Value = SmtpClient.Port;
        oFields.Update();

        //oMsg.CreateMHTMLBody("http://www.microsoft.com", CDO.CdoMHTMLFlags.cdoSuppressNone,  "", "");
        // NEED MAGIC HERE :)
        oMsg.Subject = warning.Subject;             // string

        oMsg.From = "[email protected]";
        oMsg.To = warning.EmailAddress;
        oMsg.Send();

In this snippet, the warning variable has a Body property which is a byte[]. Where it says "NEED MAGIC HERE" in the code above I want to use this byte[] to set the body of the CDO Message.

I have tried the following, which unsurprisingly doesn't work:

oMsg.HTMLBody = System.Text.Encoding.ASCII.GetString(warning.Body);

Anybody have any ideas how I can achieve what I want with CDOSYS or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说不完的你爱 2024-09-12 09:36:53

请不要使用 CDO,它可以追溯到计算机仍然使用烟雾信号来交换电子邮件的时代。 System.Net.Mail 包含您需要的一切,MailMessage 是您的朋友。请注意其 IsBodyHtml 属性。

Please don't use CDO, it dates from an era when computers still used smoke signals to exchange emails. System.Net.Mail contains everything you need, MailMessage is your friend. Note its IsBodyHtml property.

调妓 2024-09-12 09:36:53

可以通过 CDO.Message (需要添加到项目引用 COM 库“Microsoft CDO for Windows 2000 Library”):

protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
    string smtpAddress = "smtp.email.com";

    try
    {
      CDO.Message oMessage = new CDO.Message();

      // set message
      ADODB.Stream oStream = new ADODB.Stream();
      oStream.Charset = "ascii";
      oStream.Open();
      oStream.WriteText(MHTmessage);
      oMessage.DataSource.OpenObject(oStream, "_Stream");

      // set configuration
      ADODB.Fields oFields = oMessage.Configuration.Fields;
      oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
      oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
      oFields.Update();

      // set other values
      oMessage.MimeFormatted = true;
      oMessage.Subject = subject;
      oMessage.Sender = emailFrom;
      oMessage.To = emailTo;
      oMessage.Send();
    }
    catch (Exception ex)
    {
      // something wrong
    }
}

It is possible via CDO.Message (it is necessary add to project references COM library "Microsoft CDO for Windows 2000 Library"):

protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
    string smtpAddress = "smtp.email.com";

    try
    {
      CDO.Message oMessage = new CDO.Message();

      // set message
      ADODB.Stream oStream = new ADODB.Stream();
      oStream.Charset = "ascii";
      oStream.Open();
      oStream.WriteText(MHTmessage);
      oMessage.DataSource.OpenObject(oStream, "_Stream");

      // set configuration
      ADODB.Fields oFields = oMessage.Configuration.Fields;
      oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
      oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
      oFields.Update();

      // set other values
      oMessage.MimeFormatted = true;
      oMessage.Subject = subject;
      oMessage.Sender = emailFrom;
      oMessage.To = emailTo;
      oMessage.Send();
    }
    catch (Exception ex)
    {
      // something wrong
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文