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 = " "; 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; } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|