This is a simple article which tells you how you can embed images in emails. This can be very useful when you want to send emails from your website.A common scenario would be when a new user registers with your site and you want to send a welcome email with your site's logo on top.
I searched the net when I faced this issue. I am grateful to many authors from where I could make a start. I have used their code while writing this. Due credit goes to them all. The intention is to make the code available for the others too. To use this all you have to so is copy the code and paste it in any button click event.
This may only work with ASP.NET 2.0 & C#. I have tested it too. It will not run in ASP.NET 1.1.
protected void yourButton_Click(object sender, EventArgs e)
string strMailContent = "Welcome new user";
string fromAddress = "email@example.com";
string toAddress = "firstname.lastname@example.org";
string contentId = "image1";
string path = Server.MapPath(@"images/Logo.jpg"); MailMessage mailMessage = new MailMessage( fromAddress, toAddress );
mailMessage.Bcc.Add("email@example.com"); mailMessage.Subject = "Welcome new User";
LinkedResource logo = new LinkedResource(path);
logo.ContentId = "companylogo";
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);
mailMessage.IsBodyHtml = true;
SmtpClient mailSender = new SmtpClient("localhost"); mailSender.Send(mailMessage);
SmtpClient mailSender = new SmtpClient(ConfigurationManager.AppSettings["MyCustomId"]); // use this in the Production Server. I have specified my email server in the web.config file
The image may not be displayed in Outlook Express. It worked well when the mail was viewed in Yahoo. If your site is not a trusted one there is a chance that the image may be blocked. It did not work with Hotmail and Gmail.
There are other ways to show images, but like many authors have pointed out they do not embed the image. They always refer to the parent site. The image won't be displayed when offline or when the location of the image has changed in the parent site
Do please send your comments . I would be happy to hear from you.
Thank you , Happy Coding !