Click here to Skip to main content
15,392,099 members
Articles / Ports
Posted 11 Oct 2013


1 bookmarked

Sending Email with LinkedResource

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read
Introduction When you want to send email message, you must create an instances of MailMessage and SmtpClient classes. This classes are in

This articles was originally at but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.


When you want to send email message, you must create an instances of MailMessage and SmtpClient classes. This classes are in System.Net.Mail namespace. In this example aj created method GetSmtpClient which returns SmtpClient object with paramers:

  • host: represents IP or name of server user for smtp transactions
  • port: represents port used for smtp transactions
  • user: represents user name
  • password: represents password to account
  • enableSsl: specity whether use Secure Socket Layer (SSL) to encrypt the connection

Afther that I created MailMessage object. When you want to create html based message with resources (pictures ... etc), you must use LinkedResorce class. LinkedResource class represents an embedded external resource in email attachment, such as image. For every linked resource you must set ContentId which uniquely identifies resources. In example file "photo.jpg" was added with ContentId="photo".

For demostration purposes I used server.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace SendMail
    class Program
        static void Main(string[] args)
            SmtpClient smtpClient = GetSmtpClient("", 587, "", "password", true);

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("");
            mail.Subject = "Test";
            string htmlBody="";
            htmlBody = htmlBody + "<img src=\"cid:photo\">" + Environment.NewLine;
            mail.IsBodyHtml = true;
            mail.Body = htmlBody;
            AlternateView htmlview = default(AlternateView);
            htmlview = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
            LinkedResource imageResourceEs = new LinkedResource("photo.jpg");
            imageResourceEs.ContentId = "photo";
            imageResourceEs.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
            catch (Exception t)


        static SmtpClient GetSmtpClient(string host, int port, string user, string password, bool enableSsl)
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = host;
            smtpClient.Port = port;
            NetworkCredential cred = new NetworkCredential(user, password);
            smtpClient.EnableSsl = enableSsl;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Timeout = 5000;
            smtpClient.Credentials = cred;
            return smtpClient;



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ASP.NET Community
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

757 members

Comments and Discussions

-- There are no messages in this forum --