+1 vote

As I understand it, mht / mhtml files are the same as eml files except they lack fields for from, to, etc.

I have a report generated by SSRS and saved as an MHT file which I would like to email to my customers. Right now, I can do this as an attachment but it would be far better if I could just use the MHT content as the body. this would make it possible to view the reports on non-IE devices like iPads.

Is there a way to accomplish loading an MHT file and using it as the body (html email) with the mail library?

by (450 points)
limilabs, you are awesome!

1 Answer

0 votes
 
Best answer

This is possible and quite easy.

You can load mht file using:

IMail mht = new MailBuilder().CreateFromEmlFile("test.mht")

-or- mht data from memory:

IMail mht = ew MailBuilder().CreateFromEml(mht);

Then use IMail.ToBuilder method to create MailBuilder that can be used to modify the message:

IMail mht = new MailBuilder().CreateFromEmlFile("test.mht");
MailBuilder builder = mht.ToBuilder();

Remove headers defined in the mht file:

builder.From.Clear();
builder.To.Clear();
builder.Date = null;

You can also remove unnecessary headers directly from mht (before calling IMail.ToBuilder):

foreach (string header in mht.Headers.AllKeys)
{
    if (header != "content-type")
        mht.Headers.Remove(header);
}
MailBuilder builder = mht.ToBuilder();

Add correct headers:

builder.Subject = "The subject";
builder.From.Add(new MailBox("from@example.com"));
builder.To.Add(new MailBox("to@example.com"));

IMail email= builder.Create();

Finally you can send the message:

using(Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.example.com");  // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");

    smtp.SendMessage(email);                     

    smtp.Close();   
}   
by (297k points)
selected by
...