We can embed image in email body using LinkedResource class and AlternateView class that is packed with System.Net.Mail namespace. We can achieve this by making the message as HTML
.
MailMessage mail = new MailMessage();
mail.To.Add("xxx@gmail.com");
mail.From = new MailAddress("xxxx@gmail.com");
mail.Subject = "Test with Image";
string Body = "<b>Welcome to popfly!!</b><br><img alt=\"\"
hspace=0 src=\"cid:imageId\" align=baseline border=0 >";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
LinkedResource imagelink = new LinkedResource(Server.MapPath(".") +
@"\popfly.JPG", "image/jpeg");
imagelink.ContentId = "imageId";
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
mail.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.Send(mail);
No comments:
Post a Comment