Friday, 26 August 2011

C# Send email with embedded image

string mailserver = "mymailserver";
            
string subject = "Email with picture";
string body = "<html><body><h1>Picture</h1><br><img src=\"cid:Pic1\"></body></html>";
string to = "to@email.com";
string from = "from@email.com";
string imagePath = @"c:\temp\myimage.png";
            
AlternateView view = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
LinkedResource pic = new LinkedResource(imagePath, MediaTypeNames.Image.Gif);
pic.ContentId = "Pic";
view.LinkedResources.Add(pic);
            
MailMessage m = new MailMessage();
m.AlternateViews.Add(view);
            
m.From = new MailAddress(from);
m.To.Add(new MailAddress(to));
m.Subject = subject;
SmtpClient client = new SmtpClient(mailserver);
client.Send(m);

No comments:

Post a Comment