tags:

views:

910

answers:

4

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.

A: 

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 :)

jdecuyper
hello there , well not exactly , i am not trying to embed an imagei have a report (SSRS) report genirated and saved as MHTML file on specific time of day in the server i am sending it as email attachment, the question is can i send it so it be embeded in the email it self
Ali
Hi there, well you could read the MHTML into a buffer (like any kind of file) and add it the body of your email. You will maybe need to do some extra work to replace the CID contained inside the MHTML. Is that what you are trying to achieve?
jdecuyper
+2  A: 

Hi Ali,

(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.

Cheers!

Dave

dave wanta
You're very welcome, it's an excellent product :)
jdecuyper
+1  A: 

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);
Nisus
sorry but aren't you including a jpeg file ?!! i was asking about MHTML file
Ali
it's just to give an example on how to produce mime-encoded body. If you already have such a body, it's only the one way to send it - you should use CDO.Message.
Nisus
A: 

A big help after a hard week of search thanks

masoud