C # sending emails with embedded images using SmtpClient

SmtpClient () allows you to add attachments to your letters, but what if you want to make an image when the mail opens, and not attach it?

As I recall, this can be done with about 4 lines of code, but I donโ€™t remember how I canโ€™t find it on the MSDN website.

EDIT: I am not using a website or anything else, not even an IP address. Image located on the hard drive. When sent, they must be part of the mail. So, I think I could use a tag ... but I'm not sure, because my computer is not broadcasting.

+33
c # smtpclient inline
Jul 31 '09 at 14:27
source share
6 answers

One commonly mentioned solution is to add the image in the Attachment text to the mail, and then link to it in the HTML mailbox using the cid: link.

However, if you use the LinkedResources collection, inline images will still display just fine, but will not display as additional mail attachments. This is what we want to do , so here I am:

 using (var client = new SmtpClient()) { MailMessage newMail = new MailMessage(); newMail.To.Add(new MailAddress("you@your.address")); newMail.Subject = "Test Subject"; newMail.IsBodyHtml = true; var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png")); inlineLogo.ContentId = Guid.NewGuid().ToString(); string body = string.Format(@" <p>Lorum Ipsum Blah Blah</p> <img src=""cid:{0}"" /> <p>Lorum Ipsum Blah Blah</p> ", inlineLogo.ContentId); var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); view.LinkedResources.Add(inlineLogo); newMail.AlternateViews.Add(view); client.Send(newMail); } 

NOTE. . This solution adds an AlternateView to your MailMessage type text/html . For completeness, you should also add an AlternateView type text/plain containing a text version of email for non-HTML email clients.

+71
Jun 12 2018-12-12T00:
source share

The HTML email address and images are attachments, so this is just a case of accessing images (s) by their content identifiers, i.e.

  Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(txtImagePath.Text) Dim RGen As Random = New Random() A.ContentId = RGen.Next(100000, 9999999).ToString() EM.Body = "<img src='cid:" + A.ContentId +"'>" 

There are apparently comprehensive examples here: Send Email with Embedded Images

+13
Jul 31 '09 at 14:31
source share

When you say 4 lines of code, do you mean this ?

 System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepath\filename.png"); inline.ContentDisposition.Inline = true; 
+12
Jul 31 '09 at 14:33
source share

How to convert images to Base64 strings? AFAIK it can be easily integrated into the body of the letter.

Take a look here .

+2
Jul 31 '09 at 14:31
source share

The solution has already been published - this is the best I have found, I will just complete it, for example, if you have several images.

  string startupPath = AppDomain.CurrentDomain.RelativeSearchPath; string path = Path.Combine(startupPath, "HtmlTemplates", "NotifyTemplate.html"); string body = File.ReadAllText(path); //General tags replacement. body = body.Replace("[NOMBRE_COMPLETO]", request.ToName); body = body.Replace("[ASUNTO_MENSAJE]", request.Subject); //Image List Used to replace into the template. string[] imagesList = { "h1.gif", "left.gif", "right.gif", "tw.gif", "fb.gif" }; //Here we create link resources one for each image. //Also the MIME type is obtained from the image name and not hardcoded. List<LinkedResource> imgResourceList = new List<LinkedResource>(); foreach (var img in imagesList) { string imagePath = Path.Combine(startupPath, "Images", img); var image = new LinkedResource(imagePath, "image/" + img.Split('.')[1]); image.ContentId = Guid.NewGuid().ToString(); image.ContentType.Name = img; imgResourceList.Add(image); body = body.Replace("{" + Array.IndexOf(imagesList, img) + "}", image.ContentId); } //Altern view for managing images and html text is created. var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); //You need to add one by one each link resource to the created view foreach (var imgResorce in imgResourceList) { view.LinkedResources.Add(imgResorce); } ThreadPool.QueueUserWorkItem(o => { using (SmtpClient smtpClient = new SmtpClient(servidor, Puerto)) { smtpClient.EnableSsl = true; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Timeout = 50000; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new System.Net.NetworkCredential() { UserName = UMail, Password = password }; using (MailMessage mailMessage = new MailMessage()) { mailMessage.IsBodyHtml = true; mailMessage.From = new MailAddress(UMail); mailMessage.To.Add(request.ToEmail); mailMessage.Subject = "[NAPNYL] " + request.Subject; mailMessage.AlternateViews.Add(view); smtpClient.Send(mailMessage); } } }); 

As you can see, you have an array of image names, it is important that the images are in the same folder, because they pointed to the same output folder.

Finally, the message is sent as asynchronous, so the user does not need to wait for it to be sent.

0
Sep 18 '17 at 21:48 on
source share

The process of creating an image on the client when opening mail is a client function. As long as the client knows how to render the image and has not blocked any image, he will immediately open it. You do not need to do anything special by sending an email so that it is open on the client if you have correctly specified the attachment type of the mime image.

-2
Jul 31 '09 at 14:31
source share



All Articles