<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.fuzzydev.com/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>.Net : EMail</title><link>http://www.fuzzydev.com/blogs/dotnet/archive/category/1006.aspx</link><description /><dc:language>en-US</dc:language><generator>CommunityServer 2.0 (Build: 60404.2676)</generator><item><title>System.Net.Mail - How to add Alternate views and Embed Images</title><link>http://www.fuzzydev.com/blogs/dotnet/archive/2006/07/23/System_Net_Mail_AlternateView_LinkedResource.aspx</link><pubDate>Sun, 23 Jul 2006 20:05:00 GMT</pubDate><guid isPermaLink="false">daf517d6-b4b1-4612-b76e-1f60975996ca:44</guid><dc:creator>sachinjoshi</dc:creator><slash:comments>4</slash:comments><comments>http://www.fuzzydev.com/blogs/dotnet/comments/44.aspx</comments><wfw:commentRss>http://www.fuzzydev.com/blogs/dotnet/commentrss.aspx?PostID=44</wfw:commentRss><description>&lt;p&gt;
System.Net.Mail provides us with a very intuitive object model to construct and send an email message.
&lt;/p&gt; 
 
&lt;p&gt;
These days almost all the web and windows email clients gives us more control over the email content we wish to view (a.k.a HTML view, plain text view). 
This is  to make us aware that the email content we are viewing may contain explicit images or externally linked images. 
Now imagine the recipient have disabled HTML content in his/her settings then the email content will be displayed as is(with HTML tags).
&lt;/p&gt;  

&lt;p&gt;
We definitely don't want garbled text to be viewed by the recipient, now here is where the Alternate view comes to our rescue. 
As the name suggests it allows us to add alternate content that can be viewed by the receipient in case HTML content is disabled in his/her settings.
&lt;/p&gt;  

&lt;p&gt;
Similarly many web/windows email clients disable external images in the mail message to avoid linking of external images we can use the LinkedResource to embed images in our email message.
&lt;/p&gt;  

&lt;p&gt;
Although the core topic is about adding alternate views and embedding resources(images, etc) to an email, I will also touch upon configuration settings in web.config and Network Credentials.
&lt;/p&gt;  

&lt;p&gt;&lt;strong&gt;MailMessage&lt;/strong&gt;:
&lt;/p&gt;

&lt;p&gt;
The MailMessage class plays a key role by wrapping all the email details like
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From address   &lt;/li&gt;
&lt;li&gt;To address (collection)   &lt;/li&gt;
&lt;li&gt;CC address (collection)   &lt;/li&gt;
&lt;li&gt;BCC address (collection)   &lt;/li&gt;
&lt;li&gt;Email Subject   &lt;/li&gt;
&lt;li&gt;Email Body   &lt;/li&gt;
&lt;li&gt;Priority   &lt;/li&gt;
&lt;li&gt;Delivery notification options   &lt;/li&gt;
&lt;li&gt;Alternate views (Plain text, HTML)   &lt;/li&gt;
&lt;li&gt;Linked Resources (images, etc)&lt;/li&gt;
&lt;/ul&gt; 

&lt;p&gt;Below given is a typical code used to send an email, the code is pretty straight forward apart from the AlternateViews, LinkedResources and some SMTPClient config section.
&lt;/p&gt;
&lt;pre&gt;//========================================================  &lt;br&gt;MailMessage objMailMessage = new MailMessage();&lt;br&gt;&lt;br&gt;//File Attachment&lt;br&gt;if (fileUploadEmailAttachment.HasFile)&lt;br&gt;{&lt;br&gt;  objMailMessage.Attachments.Add(&lt;br&gt;  		new Attachment(&lt;br&gt;		fileUploadEmailAttachment.FileContent, &lt;br&gt;		fileUploadEmailAttachment.FileName));&lt;br&gt;&lt;br&gt;}&lt;br&gt;		&lt;br&gt;//From&lt;br&gt;objMailMessage.From = new MailAddress(txtEmailFrom.Text, &lt;br&gt;				txtDisplayName.Text);&lt;br&gt;		&lt;br&gt;//To&lt;br&gt;objMailMessage.To.Add(new MailAddress(txtEmailTo.Text));&lt;br&gt;	&lt;br&gt;//CC&lt;br&gt;if (txtEmailCC.Text.Length &gt; 0)&lt;br&gt;{&lt;br&gt; objMailMessage.CC.Add(new MailAddress(txtEmailCC.Text));&lt;br&gt;}&lt;br&gt;	&lt;br&gt;//BCC	&lt;br&gt;if (txtEmailBCC.Text.Length &gt; 0)&lt;br&gt;{&lt;br&gt; objMailMessage.Bcc.Add(new MailAddress(txtEmailBCC.Text));&lt;br&gt;}&lt;br&gt;	&lt;br&gt;//Subject&lt;br&gt;objMailMessage.Subject = txtSubject.Text;&lt;br&gt;	&lt;br&gt;//Plain Text Alternative Email Content &lt;br&gt;AlternateView objPlainAltView = AlternateView.CreateAlternateViewFromString(&lt;br&gt;					txtAlternateView.Text);&lt;br&gt;objMailMessage.AlternateViews.Add(objPlainAltView);&lt;br&gt;	&lt;br&gt;//HTML Alternative Email Content&lt;br&gt;LinkedResource objLinkedRes = new LinkedResource(&lt;br&gt;			Server.MapPath(".") &lt;br&gt;			+ "\\fuzzydev-logo.jpg", "image/jpeg");&lt;br&gt;objLinkedRes.ContentId = "fuzzydev-logo"; &lt;br&gt;	&lt;br&gt;AlternateView objHTLMAltView = AlternateView.CreateAlternateViewFromString(&lt;br&gt;				txtHTMLBody.Text, &lt;br&gt;				new System.Net.Mime.ContentType("text/html"));&lt;br&gt;	&lt;br&gt;objHTLMAltView.LinkedResources.Add(objLinkedRes);&lt;br&gt;objMailMessage.AlternateViews.Add(objHTLMAltView);&lt;br&gt;	&lt;br&gt;//High Priority&lt;br&gt;objMailMessage.Priority = MailPriority.High;&lt;br&gt;	&lt;br&gt;//Notify On Sucessfull Delivery&lt;br&gt;objMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;&lt;br&gt;SmtpClient objSMTPClient = new SmtpClient(txtHostName.Text);&lt;br&gt;objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network;&lt;br&gt;	&lt;br&gt;if(chkBoxEnableSSL.Checked)&lt;br&gt;  objSMTPClient.EnableSsl = true;&lt;br&gt;		&lt;br&gt;//Credentials (username, password)&lt;br&gt;string sUserName = txtEmailFrom.Text.Split('@')[0];&lt;br&gt;objSMTPClient.Credentials = new System.Net.NetworkCredential(&lt;br&gt;			sUserName, txtEmailFromPassword.Text);&lt;br&gt;	&lt;br&gt;//Send Email&lt;br&gt;objSMTPClient.Send(objMailMessage);&lt;br&gt;	&lt;br&gt;//========================================================&lt;br&gt;&lt;/pre&gt;  

&lt;p&gt;Now let me concentrate on the Alternateview, adding an alternate view is very easy, just create an object of the AlternateView class using the static method of the &lt;strong&gt;AlternateView&lt;/strong&gt; class called &lt;strong&gt;CreateAlternateViewFromString&lt;/strong&gt; passing the email text and the MIME type.
&lt;/p&gt;  

&lt;p&gt;&lt;strong&gt;For example if we want to create a plain text view then we will do something like this&lt;/strong&gt;
&lt;/p&gt;  

&lt;pre&gt;AlternateView objPlainAltView = AlternateView .CreateAlternateViewFromString(&lt;br&gt;					txtAlternateView.Text);&lt;br&gt;objMailMessage.AlternateViews.Add(objPlainAltView);&lt;br&gt;&lt;/pre&gt;  

&lt;p&gt;&lt;strong&gt;Now if we need HTML view then&lt;/strong&gt;
&lt;/p&gt;  

&lt;pre&gt;AlternateView objHTLMAltView = AlternateView.CreateAlternateViewFromString(&lt;br&gt;				txtHTMLBody.Text, &lt;br&gt;				new System.Net.Mime.ContentType("text/html"));&lt;br&gt;objMailMessage.AlternateViews.Add(objHTLMAltView);&lt;br&gt;&lt;/pre&gt;  

&lt;p&gt;
That looks pretty simple, now the good news is the recipient will view the email message based on the email clients settings. 
So if HTML content is disabled then plain text message is displayed otherwise the HTML content is displayed.
&lt;/p&gt; 

&lt;p&gt;Let us move forward and get our hands dirty by embedding an image in an HTML content message. 
In order to embed an image in our email we can use LinkedResource class, we can add this object to the LinkedResource collection of the AlternateView object.
&lt;/p&gt;  

&lt;pre&gt;LinkedResource objLinkedRes = new LinkedResource(&lt;br&gt;				Server.MapPath(".") &lt;br&gt;				+ "\\fuzzydev-logo.jpg", &lt;br&gt;				"image/jpeg");&lt;br&gt;objLinkedRes.ContentId = "fuzzydev-logo"; &lt;br&gt;	&lt;br&gt;AlternateView objHTLMAltView = AlternateView.CreateAlternateViewFromString(&lt;br&gt;				"&lt;img /&gt;", &lt;br&gt;				new System.Net.Mime.ContentType("text/html"));&lt;br&gt;objHTLMAltView.LinkedResources.Add(objLinkedRes);&lt;br&gt;objMailMessage.AlternateViews.Add(objHTLMAltView);&lt;br&gt;&lt;br&gt;&lt;/pre&gt;  

&lt;p&gt;
In the above code we are passing an image file path and also specifying the content type as an jpeg image to the LinkedResource object. 
Next line of code is very interesting, we are assigning a unique ContentId which will be used to set the src of our HTML image tag. 
In our case the content id is fuzzydev-logo and if you look at the HTML alternate view object in the constructor section we are having an image tag which refers to the same content id.
&lt;/p&gt;  

&lt;p&gt;
Now it's the email clients responsibility to understand the embedded resource and the reference in the image tag and display the image.
&lt;/p&gt;  

&lt;p&gt;&lt;strong&gt;SMTPClient:&lt;/strong&gt;&lt;/p&gt;  

&lt;p&gt;Now coming to the SMTP client configuration, let's just look in to what is the minimum configuration settings that SMTP client object expects from us.
&lt;/p&gt;  

&lt;ul&gt;  
&lt;li&gt;Host name or IP address   &lt;/li&gt;
&lt;li&gt;Delivery Method (Network, Pick up directory from IIS, custom  pickup directory)   &lt;/li&gt;
&lt;li&gt;EnableSsl (whether Secured Socket Layer is required)   &lt;/li&gt;
&lt;li&gt;Credentials (user name, password)&lt;/li&gt;
&lt;/ul&gt;  

&lt;p&gt;&lt;strong&gt;SMTPClient configuration in web.config:&lt;/strong&gt;&lt;/p&gt;  

&lt;pre&gt;	&lt;br&gt;	&lt;br&gt;	&lt;br&gt;	&lt;br&gt;	&lt;br&gt;		userName="email user name" &lt;br&gt;		password="email passowrd" &lt;br&gt;	/&gt;&lt;br&gt;	&lt;br&gt;	 &lt;br&gt;	&lt;br&gt;	&lt;br&gt;	&lt;br&gt;	&lt;br&gt;	 &lt;br&gt;	&lt;br&gt;	&lt;br&gt;	&lt;br&gt;&lt;/pre&gt;

&lt;p&gt;
The above configuration helps us in quickly changing the delivery method, from mail address, host name (or IP address) , user name and password. 
Although the configuration is not mandatory it is the recommended way of implementing the email functionality.
&lt;/p&gt; 

&lt;p&gt;Below given code doesn't depend on the web.config for it's configuration needs&lt;/p&gt;  

&lt;pre&gt;	SmtpClient objSMTPClient = new SmtpClient(txtHostName.Text);&lt;br&gt;	objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network;&lt;br&gt;	if(chkBoxEnableSSL.Checked)&lt;br&gt;		objSMTPClient.EnableSsl = true;&lt;br&gt;		&lt;br&gt;	//Credentials (username, password)&lt;br&gt;	string sUserName = txtEmailFrom.Text.Split('@')[0];&lt;br&gt;	objSMTPClient.Credentials = new System.Net.NetworkCredential(&lt;br&gt;				sUserName, txtEmailFromPassword.Text);&lt;br&gt;	&lt;br&gt;	//Send Email  &lt;br&gt;	objSMTPClient.Send(objMailMessage);&lt;br&gt;	&lt;br&gt;&lt;/pre&gt;	
	
&lt;p&gt;
We don't need to specify the host name, etc if it's available in the web.config, this is usefull as in different part's of our application we can just use the specified configuration and if required then specify manually.
&lt;/p&gt;  

&lt;p&gt;
Now that deserves a round applause for those involved in the development of System.Net.Mail classes.
&lt;/p&gt;&lt;img src="http://www.fuzzydev.com/aggbug.aspx?PostID=44" width="1" height="1"&gt;</description><category domain="http://www.fuzzydev.com/blogs/dotnet/archive/category/1000.aspx">ASP.Net</category><category domain="http://www.fuzzydev.com/blogs/dotnet/archive/category/1006.aspx">EMail</category></item></channel></rss>