0

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 = "system@example.com";
        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?

2 Answers2

0

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.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • I did look at System.Net.Mail and MailMessage but it doesn't seem to support MHTML. And isn't it just a wrapper for CDOSYS anyway? CDOSYS at least seemed to have *some* support for MHTML, but it seems you have to use CDOSYS to generate the MHTML which is not really what I want. –  Jun 08 '10 at 02:56
  • You might be better off working from the assumption that few people that read this know what MHTML means. – Hans Passant Jun 08 '10 at 11:32
0

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
    }
}
petr3petr
  • 19
  • 2
  • This works. Don't let the accepted answer fool you into believing you should use MailMessage. I have not been able to figure out a way to do this without CDO yet (and without paying for commerical software). – thomas Jun 23 '15 at 19:00