Hi nadeem1218,
In the emailtemplate html you need to provide hosted image url instead of local path.
Upload image file to any web server and then use the image URL in the HTML body for that image.
Or use place holder for image src and from code behind convert the path to base64 string and replace the place holder with this base64 string.
Refer below example.
EmailTemplate
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<img src="{LogoUrl}" />
<br /><br />
<div style="border-top: 3px solid #B49F0C"> </div>
<span style="font-family:Arial;font-size:10pt">
Hello <b>{UserName}</b>,<br /><br />
<a style="color: #B49F0C" href="{Url}"> Welcome to our Family</a><br /><br />
Your free 10% discount coupon is: {Description}
<br />
Valid to be applied on any of our products except the discounted products.
<br /><br />
Thanks,<br />
</span>
</body>
</html>
HTML
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="SendEmail" />
Namespaces
using System.Net.Mail;
using System.Configuration;
using System.IO;
Code
protected void SendEmail(object sender, EventArgs e)
{
string body = this.PopulateBody("John",
"Fetch multiple values as Key Value pair in ASP.Net AJAX AutoCompleteExtender",
"http://www.aspsnippets.com/Articles/Fetch-multiple-values-as-Key-Value-pair-" +
"in-ASP.Net-AJAX-AutoCompleteExtender.aspx",
"Here Mudassar Ahmed Khan has explained how to fetch multiple column values i.e." +
" ID and Text values in the ASP.Net AJAX Control Toolkit AutocompleteExtender"
+ "and also how to fetch the select text and value server side on postback");
this.SendHtmlFormattedEmail("recipient@gmail.com", "New article published!", body);
}
private string PopulateBody(string userName, string title, string url, string description)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{LogoUrl}", this.GetImageUrl("Images/Penguins.jpg"));
body = body.Replace("{UserName}", userName);
body = body.Replace("{Title}", title);
body = body.Replace("{Url}", url);
body = body.Replace("{Description}", description);
return body;
}
private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(recepientEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mailMessage);
ClientScript.RegisterStartupScript(this.GetType(),"alert","alert('Mail sent SuccessFully')",true);
}
}
private string GetImageUrl(string imagePath)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(imagePath));
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
Byte[] bytes = new Byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(bytes, 0, (int)bytes.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
string imageUrl = "data:image/png;base64," + base64String;
return imageUrl;
}