Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending mhtml emails - C#

Tags:

c#

.net

email

mhtml

I have a requirement to send emails containing both text and Images.
So, I have .mhtml file that contains the content that needs to be emailed over.

I was using Chilkat for this, but in outlook 2007 it is showing the mhtml file as different attachments(html+images).

Can anyone suggest me some other component for sending mhtml emails.
FYI, I am using .Net 3.5

Also, I do not want to save the images on server before sending them.

Thank you!

like image 888
inutan Avatar asked Jan 20 '10 11:01

inutan


2 Answers

I use plain old native MailMessage class. This previous answer can point you in right direction

EDIT: I built a similiar code some time ago, which captures an external HTML page, parse it's content, grab all external content (css, images, etc) and to send that through email, without saving anything on disk.

like image 137
Rubens Farias Avatar answered Nov 15 '22 15:11

Rubens Farias


Here is an example using an image as an embedded resource.

MailMessage message = new MailMessage();
message.From = new MailAddress(fromEmailAddress);
message.To.Add(toEmailAddress);
message.Subject = "Test Email";
message.Body = "body text\nblah\nblah";

string html = "<body><h1>html email</h1><img src=\"cid:Pic1\" /><hr />" + message.Body.Replace(Environment.NewLine, "<br />") + "</body>";
AlternateView alternate = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
message.AlternateViews.Add(alternate);

Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream("SendEmailWithEmbeddedImage.myimage.gif")) {
    LinkedResource picture = new LinkedResource(stream, MediaTypeNames.Image.Gif);

    picture.ContentId = "pic1"; // a unique ID 
    alternate.LinkedResources.Add(picture);

    SmtpClient s = new SmtpClient();
    s.Host = emailHost;
    s.Port = emailPort;
    s.Credentials = new NetworkCredential(emailUser, emailPassword);
    s.UseDefaultCredentials = false;

    s.Send(message);
}
}
like image 31
Tom Brothers Avatar answered Nov 15 '22 15:11

Tom Brothers