使用嵌入在body html中的图像发送电子邮件c#

时间:2018-03-20 15:00:30

标签: c# outlook

我使用此方法发送包含正文中嵌入图像的电子邮件,除了在Outlook中,它几乎适用于所有客户端。它没有显示图像前景,但它在Outlook网站中显示。有人帮忙吗?

这里有方法

public static void SendNotificationMail(string subject, string message, string emailTo, string ccTo, string bccTo, string filename)
    {
        try
        {
            // Create a message and set up the recipients.
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(AppConfig.EmailFrom, AppConfig.EmailFromDisplayName);
            msg.To.Add(emailTo);
            if (!string.IsNullOrEmpty(ccTo))
            {
                msg.CC.Add(ccTo);
            }
            if (!string.IsNullOrEmpty(bccTo))
            {
                msg.Bcc.Add(bccTo);
            }
            msg.Subject = subject;
            msg.Body = message;
            // Create  the file attachment for this e-mail message.
            //string contentID = "@std";
            String imagesPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "images";
            String[] files = Directory.GetFiles(imagesPath);
            foreach(String imageFile in files){
                FileInfo file = new FileInfo(imageFile);
                // create the INLINE attachment
                Attachment inline = new Attachment(file.FullName);
                inline.ContentDisposition.Inline = true;
                inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
                inline.ContentId = "@"+file.Name;
                inline.ContentType.MediaType = "image/jpg";
                inline.ContentType.Name = Path.GetFileName(imageFile);
                //Utility.PrintMessage("Attachment" + file.Name + " ==> " + file.FullName);
                msg.Attachments.Add(inline);
                // replace the tag with the correct content ID
                //msg.Body = msg.Body.Replace("@@IMAGE@@", "cid:" + contentID);
            }
            msg.IsBodyHtml = true;
            //Send the message.
            SmtpClient client = new SmtpClient(AppConfig.SMTPServer);
            // Add credentials if the SMTP server requires them.
            client.Credentials = CredentialCache.DefaultNetworkCredentials;
            try
            {
                Utility.PrintMessage("Sending notification e-mail to: " + emailTo);
                client.Send(msg);
                Logger.WriteTNSLog(subject + " - " + emailTo);
            }
            catch (Exception e)
            {
                Logger.WriteAppLog(e.Message, "Utility.SendNotificationMail()");
            }
        }
        catch (Exception)
        {
        }
    }

0 个答案:

没有答案