Home > Forum > C# > How to send email with banner as header with HTML format

How to send email with banner as header with HTML format

by Thamil   on Oct 13, 2014   Category: C#  |  Views: 1776    |  Points: 5   |  Gold 
  Reply
Hi All,

I am sending mail to my client through code, but I could not send banner image my code. Banner image located in my web server(Local directory). I have tried to attach my banner along with my mail, but it's not working.
Please share the code, how to send mail with banner or image(using inner attachment) with HTML format.

THanks in advance.

Question posted on behalf of Rajan.B

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :How to send email with banner as header with HTML format   
by Thamil
on Oct 13 2014 5:15AM
Points : 10
Gold 
Hi Rajan.B

Below sample code is used to send a email with images like header, logo by using LinkedResource & AlternateView (instead of using Mailmessage.body), and we can embed a image in a email attachment.
protected void Send_Click(object sender, EventArgs e)
{
string strSMTP = "*********";
int Port = **;
MailAddress from = new MailAddress("test1@test.com");
MailAddress to = new MailAddress("test2@test.com");
MailMessage msg = new MailMessage(from, to);

AlternateView avView = AlternateView.CreateAlternateViewFromString(EmailBody(), null, MediaTypeNames.Text.Html);

string Imagepath = Server.MapPath(@"Images/header.jpg");

LinkedResource Lr = new LinkedResource(Imagepath, MediaTypeNames.Image.Jpeg);
Lr.ContentId = "Header";
avView.LinkedResources.Add(Lr);

msg.AlternateViews.Add(avView);

msg.Subject = "Test Subject";
msg.BodyEncoding = Encoding.ASCII;
msg.IsBodyHtml = true;
msg.SubjectEncoding = Encoding.UTF8;
SmtpClient smtp = new SmtpClient(strSMTP, Port);
smtp.UseDefaultCredentials = true;
smtp.Send(msg);
}

public string EmailBody()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<html>");
sb.AppendLine("<body>");
sb.AppendLine("<div><img src=\"cid:Header\"></div><br>");
sb.AppendLine("<p>Hello All, test mail</p>");
sb.AppendLine("<hr>");
sb.AppendLine("<p><i> *** This is an automatically generated email, please do not reply *** </i></p>");
sb.AppendLine("</body>");
sb.AppendLine("<html>");
return sb.ToString();
}

Recent Post

Latest Posts