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

Time:2020-1-17

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.

Summary

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 pictures in the article are usually the embedded resources in the pictures, so they will not appear in the attachment list of the mail.

practice

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.

Preparation

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 implementation steps mainly ask that the assembly body is wrapped in alternative, and then the alternative and embedded resources are wrapped in related, and then the related is wrapped with mixed package to copy the above content to the mail body.

The link of the picture is processed in format, the original:Use mailkit to send an email with embedded pictures that do not appear as attachmentsModify to。 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)
        {
            try
            {
                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);
                    client.Send(message);
                    client.Disconnect(true);
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

 

Mail received after sending