Ali
Ali

Reputation: 595

sending inline MHTML

I was wondering if it is possible through the .NET 2.0 MailMessage object to send an inline MHTML file that is created on the fly.

By inline I mean: It should be sent in a way that the user can see it, once he opens the email, without having to open/download the attachment.

Upvotes: 2

Views: 7745

Answers (5)

dave wanta
dave wanta

Reputation: 7214

(jdecuyper -- thanks for the plug, as I wrote aspNetEmail).

You can do this with aspNetEmail. You can replace the entire contents of the email message with your MHT.

You can't do this with System.Net.Mail, but if you want to go the commerical route, pop me an email at [email protected] and I'll show you how this can be done.

If you want to go an open source route, there is probably some SMTP code on codeproject that you could modify to do this. Basically, you would inject your contents into the DATA command of the SMTP process.

One thing to note: If your MHT document has embedded scripts, flash, activeX objects or anything that may be blocked by the mail client, it probably won't render the same as what you are seeing in the browser.

Upvotes: 3

petr3petr
petr3petr

Reputation: 19

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

Upvotes: 0

petr3petr
petr3petr

Reputation: 19

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

Upvotes: 0

Nisus
Nisus

Reputation: 824

It's a bit tricky, but yes, you can do it. In fact MailMessage class is nothing more than a wrapper above the system's CDO.Message class which can do the trick. Also you can use AlternateView functionality, it's more simple:

MailMessage mailMessage = new MailMessage("[email protected]"
    ,"[email protected]"
    ,"test"
    ,"");
string ContentId = "wecandoit.jpg";
mailMessage.Body = "<img src=\"cid:" + ContentId + "\"/>";
AlternateView av = AlternateView.CreateAlternateViewFromString(mailMessage.Body
    ,null
    ,MediaTypeNames.Text.Html);
LinkedResource lr = new LinkedResource(@"d:\Personal\My Pictures\wecandoit.jpg");
lr.ContentId = ContentId;
lr.ContentType.Name = ContentId;
lr.ContentType.MediaType = "image/jpeg";
av.LinkedResources.Add(lr);
mailMessage.AlternateViews.Add(av);
SmtpClient cl = new SmtpClient();
cl.PickupDirectoryLocation = @"c:\test";
cl.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
cl.Send(mailMessage);

Upvotes: 3

jdecuyper
jdecuyper

Reputation: 3963

Are you trying to add some images to an html email?

To accomplish this you will need to embed the images inside your email. I found a tutorial to accomplish it in a few lines of code. You can also buy the aspnetemail assembly. It has always helped me a lot to send emails with embedded images, they also have an excellent support team if anything goes wrong.

Keep in mind that embedding images makes your email heavier, but nicer :)

Upvotes: 1

Related Questions