Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Sending Email with LinkedResource

0.00/5 (No votes)
21 Mar 2010 1  
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 wiki.asp.net 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.

Introduction 

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 smtp.gmail.com 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("smtp.gmail.com", 587, "GmailUserName@gmail.com", "password", true);


            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("GmailUserName@gmail.com");
            mail.To.Add("AddressTo@hotmail.com");
            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;
            htmlview.LinkedResources.Add(imageResourceEs);
            mail.AlternateViews.Add(htmlview);
            try
            {
                smtpClient.Send(mail);
            }
            catch (Exception t)
            {
                Console.WriteLine(t.Message);
            }

        }

        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;
        }
    }
}


 Links

http://msdn.microsoft.com/en-us/library/system.net.mail.linkedresource.aspx 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here