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);

Comments

Popular posts from this blog

Parse XML to dynamic object in C#

C# Updating GUI from different thread