HTML E-mail with images

The code sample sends HTML E-Mail messages that can include images from a C# program.

We can Use AlternateView and LinkedResource classes to create an html email and embed the image as a linked resource instead of attachment. we can also have to change the url / src of the image to the format "cid:ContentId". It will work for both img and background. Here is a code


//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("user@gmail.com");
mail.To.Add("you@gmail.com");

//set the content
mail.Subject = "Test mail";

//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my text , viewable by those clients that don't support html", null, "text/plain");

//then we create the Html part
//to embed images, we need to use the prefix 'cid' in the img src value
//the cid value will map to the Content-Id of a Linked resource.
//thus "" will map to a LinkedResource with a ContentId of 'logo'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image."", null, "text/html");

//create the LinkedResource (embedded image)
LinkedResource logo = new LinkedResource( "c:\\temp\\companylogo.gif" );
logo.ContentId = "logo";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);

//add the views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);


//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);



Comments

Author: Nirupa Rani Acharya27 Jul 2008 Member Level: Bronze   Points : 1

please send javascript code for submitting a page without using a submit button but using an image on the login window..

Author: john14 Jun 2010 Member Level: Bronze   Points : 2

Pro: Upgrading Your Enterprise Support (70-621) 70-680 certification exam is the one, if the need to obtain a 000-931 certification, you may also need to attend other relevant examinations, MCTS Certification, please visit the topic, where you will see all the relevant MCTS certification exam subjects. 70-663 certification is the certification authority, one has a pivotal position in the international. If you have a 000-939 certification, you enter the IT industry coupled with the power company will a heavy weight. The certificate holders will enjoy worldwide free credit 000-973 relevant professional treatment, some countries in North America as immigrants technical evaluation criteria. Vrf Obtain any level of 70-662 certification means you are one of influential figure IT industry.

Author: Er. Ram Singh06 Sep 2010 Member Level: Gold   Points : 2

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Net.Mail;public partial class Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) {//Calling the function SendMail Response.Write( SendMail("meetuchoudhary@gmail.com","meetudmeet@gmail.com","meetudmeet@yahoo.com","Test Mail","Test Mail Body")); } public string SendMail(string toList, string from, string ccList, string subject, string body) { MailMessage message = new MailMessage(); SmtpClient smtpClient = new SmtpClient(); string msg = string.Empty; try { MailAddress fromAddress = new MailAddress(from); message.From = fromAddress; message.To.Add(toList); if (ccList != null && ccList != string.Empty) message.CC.Add(ccList); message.Subject = subject; message.IsBodyHtml = true; message.Body = body; smtpClient.Host = "mail.server.com"; smtpClient.Port = 25; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password"); smtpClient.Send(message); msg = "Successful"; } catch (Exception ex) { msg = ex.Message; } return msg; }}

Author: stevemartyn01 Oct 2010 Member Level: Bronze   Points : 1

Please send javascript code for submitting a page without using a submit button but using an image on the login window. Please send javascript code for submitting a page without using a submit button but using an image on the login window.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: