Use MailKit to send an email with embedded pictures that do not appear as attachments

Use MailKit to send an email with embedded pictures that do not appear as attachments

Reference article: MailKit --- send mail

Be careful

Whether to display embedded pictures as attachments in the mail client is different according to different mails. Outlook and qq are not displayed as attachments after testing temporarily, and Sina mailbox will display attachments.


To send an email with complex format and complex pictures in the body content, take an email sent by Alibaba cloud as an example, including 5 pictures. Now you want to use the MailKit library to send mail. The mail template also contains pictures. How to send them?

Knowledge points

MIME Multipurpose Internet mail extension type

An email with complex content can be shown in the following figure, which is divided into three parts: alternative, related and mixed. The alternative part contains plain text and hypertext content, the related part contains alternative part and embedded resources, and the mixed part contains related part and attachment.



The picture in the article is generally the embedded resource in the picture, so it will not appear in the attachment list of the mail.


Now we use this Alibaba cloud email as a template and use MailKit to send this email, and the effect of the received email is the same.


Download the email locally, save it as an Html file, download the image resources used in the email, and put the Html file and image in one file as a template at a time.

code implementation

The main step of the implementation is to wrap the body of the assembly in alternative, then wrap alternative and embedded resources in related, and then wrap related with mixed package to copy the above content to the body of the mail.

The link of the picture is format processed. The original: < img SRC = "XXX / xxx / XX. JPG" / > is modified to < img SRC = "CID: aaaaaaaa" / >. See shooting ContentId in the code for details.

Use bodyBuilder to build the message body. See code for details.

        public bool SendEmailToSingle(string toEmail, string Subject, string FormatId)
                var message = new MimeMessage();
                message.To.Add(new MailboxAddress(toEmail));
                message.From.Add(new MailboxAddress(EmailDisplayName, EmailUserName));
                message.Subject = Subject;

                // Get mail template
                string FullFormatPath = Path.Combine(Environment.CurrentDirectory, "Content", FormatId);
                string[] ImgPaths = Directory.GetFiles(Path.Combine(FullFormatPath, "Resources"));

                string HtmlFormat = string.Empty;

                var builder = new BodyBuilder();

                using (FileStream fs = new FileStream(Path.Combine(FullFormatPath, "format.htm"), FileMode.Open))
                    using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gbk")))
                        HtmlFormat = sr.ReadToEnd();

                // Add pictures to embedded resources and replace links to pictures in the message
                foreach (string imgpath in ImgPaths)
                    var image = builder.LinkedResources.Add(imgpath);
                    image.ContentId = MimeUtils.GenerateMessageId();
                    HtmlFormat = HtmlFormat.Replace(Path.GetFileName(imgpath), string.Format("cid:{0}", image.ContentId));

                builder.HtmlBody = HtmlFormat;

                message.Body = builder.ToMessageBody();

                //return message;
                using (SmtpClient client = new SmtpClient())
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    client.Connect(EmailServerAddress, EmailServerPort, false);
                    client.Authenticate(EmailUserName, EmailPassword);

                return true;
            catch (Exception)
                return false;


Mail received after sending

Keywords: C# encoding

Added by XeoWolf on Sun, 20 Oct 2019 13:41:04 +0300