C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Email »

HTML E-mail with images


Posted Date: 23 Jul 2008    Resource Type: Code Snippets    Category: Email
Author: Sajid P KMember Level: Gold    
Rating: 1 out of 5Points: 8



The code sample sends HTML E-Mail messages that can include images from a C# program.

We can Use AlternateView and LinkedResource classes to create an html email and embed the image as a linked resource instead of attachment. we can also have to change the url / src of the image to the format "cid:ContentId". It will work for both img and background. Here is a code


//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("user@gmail.com");
mail.To.Add("you@gmail.com");

//set the content
mail.Subject = "Test mail";

//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my text , viewable by those clients that don't support html", null, "text/plain");

//then we create the Html part
//to embed images, we need to use the prefix 'cid' in the img src value
//the cid value will map to the Content-Id of a Linked resource.
//thus "" will map to a LinkedResource with a ContentId of 'logo'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image."", null, "text/html");

//create the LinkedResource (embedded image)
LinkedResource logo = new LinkedResource( "c:\\temp\\companylogo.gif" );
logo.ContentId = "logo";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);

//add the views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);


//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);






Responses

Author: Nirupa Rani Acharya    27 Jul 2008Member Level: Bronze   Points : 1
please send javascript code for submitting a page without using a submit button but using an image on the login window..


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Image  .  HTML Email messages from C#  .  HTML Email messages  .  HTML Email message with Images  .  HTML Email message in C#  .  HTML Email message containing Images  .  HTML Email message  .  Email  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sending Inline Attachments Thru E-Mail
Previous Resource: Send Exceptions or Error in Email using Log4Net
Return to Discussion Resource Index
Post New Resource
Category: Email


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use