MailMessage c# - How to make it HTML and add images etc?
On the MailMessage
set the property IsBodyHtml
to true.
string to = "email@hotmail.co.uk";
string body = "Test";
SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
MailMessage mailObj = new MailMessage(urEmail, to, subject, body);
mailObj.IsBodyHtml = true; // This line
SMTPServer.Send(mailObj);
sending mail along with embedded image using asp.net
If you are using .NET 2 or above you can use the AlternateView and LinkedResource classes like this:
string html = @"<html><body><img src=""cid:YourPictureId""></body></html>";
AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg);
yourPictureRes.ContentId = "YourPictureId";
altView.LinkedResources.Add(yourPictureRes);
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(altView);
Hopefully you can deduce the VB equivalent.
How to embed multiple images in email body using .NET
So, I think figured out what the actual problem is
Its in this line
// Alternate view for embedded images
AlternateView avText = AlternateView.CreateAlternateViewFromString(metric.Name, null, MediaTypeNames.Text.Html);
AlternateView avImages = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
As you can see, both my views are specified as Text.Html, so the the 1st one is overriding the next one and so I only see text and images are sent as attachments
I made the following change and it worked as expected
AlternateView avText = AlternateView.CreateAlternateViewFromString(metric.Name, null, **MediaTypeNames.Text.Plain**);
AlternateView avImages = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
Generating HTML email body in C#
You can use the MailDefinition class.
This is how you use it:
MailDefinition md = new MailDefinition();
md.From = "test@domain.example";
md.IsBodyHtml = true;
md.Subject = "Test of MailDefinition";
ListDictionary replacements = new ListDictionary();
replacements.Add("{name}", "Martin");
replacements.Add("{country}", "Denmark");
string body = "<div>Hello {name} You're from {country}.</div>";
MailMessage msg = md.CreateMailMessage("you@anywhere.example", replacements, body, new System.Web.UI.Control());
Also, I've written a blog post on how to generate HTML e-mail body in C# using templates using the MailDefinition class.
embedding image in html email
Try to insert it directly, this way you can insert multiple images at various locations in the email.
<img src="data:image/jpg;base64,{{base64-data-string here}}" />
And to make this post usefully for others to:
If you don't have a base64-data string, create one easily at:
http://www.motobit.com/util/base64-decoder-encoder.asp from a image file.
Email source code looks something like this, but i really cant tell you what that boundary thing is for:
To: email@email.de
Subject: ...
Content-Type: multipart/related;
boundary="------------090303020209010600070908"
This is a multi-part message in MIME format.
--------------090303020209010600070908
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">
</head>
<body bgcolor="#ffffff" text="#000000">
<img src="cid:part1.06090408.01060107" alt="">
</body>
</html>
--------------090303020209010600070908
Content-Type: image/png;
name="moz-screenshot.png"
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline;
filename="moz-screenshot.png"
[base64 image data here]
--------------090303020209010600070908--
//EDIT: Oh, i just realize if you insert the first code snippet from my post to write an email with thunderbird, thunderbird automatically changes the html code to look pretty much the same as the second code in my post.
Related Topics
What Is The Story of Performance Counters for .Net Core
Calling an Overridden Method from a Parent Class Ctor
How to Stop an Application from Opening
ASP.NET MVC 4 + Ninject MVC 3 = No Parameterless Constructor Defined for This Object
How to Share an Enum Declaration Between C# and Unmanaged C++
Generate PDF from ASP.NET from Raw HTML/CSS Content
How to Programmatically Apply a CSS Class to an ASP.NET Control
How to Remove Specific Style Tag in HTML Using C#
Get Text Between 2 HTML Tags C#
Is Correct to Use Gc.Collect(); Gc.Waitforpendingfinalizers();
Attach Debugger to Iis Instance
HTMLagilitypack HTMLweb.Load Returning Empty Document
Getting Value from HTML Radio Button - in Aspx-C#
Convert from Word Document to HTML