C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Send email with embedded images


Posted Date: 25 Feb 2007    Resource Type: Code Snippets    Category: Email

Posted By: Sidhartha Gundavarapu       Member Level: Gold
Rating:     Points: 10



This code snippet sends email with embedded images in the body and using the SMTP

							
public bool SendRichMail
(
string[] toAddresses,
string subject
)
{
if (
(toAddresses == null) ||
(toAddresses.Length == 0)
)
return false;

//parity checks and customized messages
string FromAddress = "myname@myserver.com"; //change this to some your email address
string SmtpHost = "smtphost.contoso.com"; //change this to the real smtp server address

string message =
"\"image ";

LinkedResource title = new LinkedResource( "title.jpg" ); //this file has to be there in the same directory as this assembly
title.ContentId = "Title"; //this has to match the cid in the message and is not case sensitive

//create an alternate view
AlternateView view = AlternateView.CreateAlternateViewFromString
(
body,
null,
System.Net.Mime.MediaTypeNames.Text.Html
);

//link the resources
view.LinkedResources.Add( title );

//attach the from address
msg.From = new MailAddress( FromAddress );

//attach the to addresses
try
{
foreach ( string mailId in toAddresses )
msg.To.Add( mailId );
}
catch ( Exception e ) //do not catch Exception. I'm doing this as I'm throwing back after logging
{
Log( e ); //this is a typical logging code and is not attached
throw e;
}

//attach the alternate view to the message
msg.AlternateViews.Add( view );
msg.Subject = subject;
msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.Host = SmtpHost;

//using the default credentials
client.UseDefaultCredentials = true;

//if you are using any other credentials then use this
//string UserName = "username";
//string Password = "password";
//string Domain = "domain";
//client.Credentials = new NetworkCredential( UserName, Password, Domain );

try
{
client.Send( msg );
}
catch ( Exception e ) //do not catch Exception. I'm doing this as I'm throwing back after logging
{
Log( e ); //this is a typical logging code and is not attached
throw e;
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

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 eMail using SmtpClient class
Previous Resource: Send Email programmatically using your Gmail account
Return to Discussion Resource Index
Post New Resource
Category: Email


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

fax server

Contact Us    Privacy Policy    Terms Of Use