Sending and receiving emails is a daily task for almost every professional, and programmers are not different. Sending emails and attachment is considered a trivial task, and all programming platforms support this feature.
You can send an email with images as external links, but most email clients block external links. There is another way around to send images as part of an email.
Using the code
The following code is self explanatory. Here, we go:
- Create a string that contains the HTML message to send.
- Create an
AlternateView object for supporting the HTML.
- Create a
LinkedResource object for the image to send.
- Add a
LinkedResource object to the
- Create a
Mailmesasge object and set its
- Add an
AlternateView object to the
- Create an
SmtpClient object and send the
string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:Pic1\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
LinkedResource pic1 = new LinkedResource("pic.jpg", MediaTypeNames.Image.Jpeg);
pic1.ContentId = "Pic1";
MailMessage m = new MailMessage();
m.From = new MailAddress("firstname.lastname@example.org", "Rizwan Qureshi");
m.To.Add(new MailAddress("email@example.com", "Shayan Qureshi"));
m.Subject = "A picture using alternate views";
SmtpClient client = new SmtpClient("smtp.dotnetplayer.com");
Points of interest
Since embedded images are part of emails, they can considerably increase the size of emails and can cause an email client to treat them as spam.
I am a man with distinguish academic and professional career. i love programming and it is next to breathing to me. I did my post graduation in Computer Science and specialized in the field of Artificial Intelligence. these days i am busy in developing a developer friendly site. I am open to any suggestion and would definitely seek help in this regard.
Here are few
certifications i earned:
MCPD Enterprise Application Development Framework 2.0
MCTS: Framwork Fundamentals 2.0
MCTS: Web Client Development 2.0
MCTS: Window Client Development 2.0
MCTS: Distributed Client Development 2.0
MCITP:Microsoft® SQL Server™ 2005 - Implementation and Maintenance
MCTS:Microsoft Windows SharePoint Services 3.0 - Application Development
MCTS:Developing Business Process and Integration Solutions using Microsoft® BizTalk® Server 2006
MCTS:Microsoft SQL Server 2005 Business Intelligence–Implementation and Maintenance
I wish my son would earn more certification when we celebrate his sixth birthday...
you can reach me at firstname.lastname@example.org