【问题标题】:Embedded images getting removed by antivirus嵌入式图像被防病毒软件删除
【发布时间】:2016-08-01 12:03:52
【问题描述】:

我使用以下代码发送带有嵌入图像的电子邮件,但是它导致了一个问题,即某些消息与以前消息中的图像一起发送。

using (MailMessage msg = new MailMessage())
            {
                msg.From = new MailAddress(mailFrom);
                msg.To.Add(mail);
                msg.Subject = assunto;
                msg.Body = MSG;
                msg.IsBodyHtml = true;

                //create Alternative HTML view
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(MSG, null, "text/html");
                //Add Image - logo
                LinkedResource theEmailImage = null;

                switch (TYPE_MAIL)
                {
                    case "1":
                    case "2":
                    case "3":
                        theEmailImage = new LinkedResource(ImagePath + "logo_rgb.jpg");
                        break;

                    default:
                        theEmailImage = new LinkedResource(ImagePath + "header_report_pt_700.jpg");
                        break;
                }

                theEmailImage.ContentId = "logo";


                //Add the Image to the Alternate view
                htmlView.LinkedResources.Add(theEmailImage);

                //Add Images - email (limit 8)
                LinkedResource img1;
                int count = 0;

                switch (TYPE_MAIL)
                {
                    case "1":
                        string image = "";
                        for (int i = 0; i < 3; i++)
                        {
                            try
                            {
                                image = Images[i].ToString();
                                img1 = new LinkedResource(AttachmentsPath + image);
                            }
                            catch (Exception)
                            {
                                img1 = new LinkedResource(AttachmentsPath + "empty.png");
                            }

                            img1.ContentId = "img" + (i + 1);
                            htmlView.LinkedResources.Add(img1);
                        }

                        break;
                    case "2":

                        foreach (string imagem in Images)
                        {
                            try
                            {
                                img1 = new LinkedResource(AttachmentsPath + imagem);
                            }
                            catch (Exception)
                            {
                                img1 = new LinkedResource(AttachmentsPath + "empty.png");
                            }

                            img1.ContentId = "img" + count;

                            //Add the Image to the Alternate view
                            htmlView.LinkedResources.Add(img1);
                            count++;
                        }

                        break;
                    default:
                        break;
                }

                //Add views to the Email Message (all images)
                msg.AlternateViews.Add(htmlView);

                try
                {
                    //RELAY (configuration on App.config)
                    using (SmtpClient smtp = new SmtpClient(ServidorSmtp))
                    {
                        smtp.Send(msg);
                        result = "OK";
                    }
                }
                catch (Exception ex)
                {
                    cLog.addItemToLog("E-MAIL FAILED! " + DateTime.Today.ToString("dd-MM-yyyy") + " - " + ex);
                }
            }

然后,由于我的假设是问题与未处理的对象有关,因此我将其稍微更改为:

using (var msg = new MailMessage())
            {
                msg.From = new MailAddress(mailFrom);
                msg.To.Add(mail);
                msg.Subject = assunto;
                msg.Body = MSG;
                msg.IsBodyHtml = true;

                //create Alternative HTML view
                using (AlternateView htmlView = AlternateView.CreateAlternateViewFromString(MSG, null, "text/html"))
                {
                    //Add Image - logo
                    LinkedResource theEmailImage = null;

                    switch (TYPE_MAIL)
                    {
                        case "1":
                        case "2":
                        case "3":
                            theEmailImage = new LinkedResource(ImagePath + "logo_rgb.jpg");
                            break;

                        default:
                            theEmailImage = new LinkedResource(ImagePath + "header_report_pt_700.jpg");
                            break;
                    }

                    theEmailImage.ContentId = "logo";


                    //Add the Image to the Alternate view
                    htmlView.LinkedResources.Add(theEmailImage);

                    //Add Images - email (limit 8)
                    LinkedResource img1;
                    int count = 0;

                    switch (TYPE_MAIL)
                    {
                        case "1":
                            string image = "";
                            for (int i = 0; i < 3; i++)
                            {
                                try
                                {
                                    image = Images[i].ToString();
                                    img1 = new LinkedResource(AttachmentsPath + image);
                                }
                                catch (Exception)
                                {
                                    img1 = new LinkedResource(AttachmentsPath + "empty.png");
                                }

                                img1.ContentId = "img" + (i + 1);
                                htmlView.LinkedResources.Add(img1);
                            }

                            break;
                        case "2":

                            foreach (string imagem in Images)
                            {
                                try
                                {
                                    img1 = new LinkedResource(AttachmentsPath + imagem);
                                }
                                catch (Exception)
                                {
                                    img1 = new LinkedResource(AttachmentsPath + "empty.png");
                                }

                                img1.ContentId = "img" + count;

                                //Add the Image to the Alternate view
                                htmlView.LinkedResources.Add(img1);
                                count++;
                            }

                            break;
                        default:
                            break;
                    }

                    //Add views to the Email Message (all images)
                    msg.AlternateViews.Add(htmlView);

                    try
                    {
                        //RELAY (configuration on App.config)
                        using (var smtp = new SmtpClient(ServidorSmtp))
                        {
                            smtp.Send(msg);
                            result = "OK";
                        }
                    }
                    catch (Exception ex)
                    {
                        cLog.addItemToLog("E-MAIL FAILED! " + DateTime.Today.ToString("dd-MM-yyyy") + " - " +
                                          ex);
                    }
                }
            }

现在,有些电子邮件在发送时没有嵌入图像,&lt;img&gt;标签存在但图像不存在。

现在我的假设是邮件服务器阻止了一些嵌入的图像,但我不知道如何测试。

有什么建议吗?

更新

这是邮件 HTML 的要点(必须编辑邮件以删除机密信息,但 Outlook 不允许我将其保存回 .EML):https://gist.github.com/julianonunes/477362fe505bd3e49b31038472ae7403

【问题讨论】:

    标签: c# asp.net mailmessage


    【解决方案1】:

    为了避免服务器的复杂性,您可以使用以下代码在本地传递消息以进行测试:

            SmtpClient client = new SmtpClient("myhost");
            client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
            client.PickupDirectoryLocation = @"C:\temp";
            client.Send(msg);
    

    【讨论】:

    • 但是这样只会阻止邮件投递到用户邮箱,还是我错了?
    • 我已经像你提到的那样设置了配置,并且有些消息是在没有附加图像的情况下创建的。但是我在日志文件中没有任何异常。似乎并非所有图像都在加载。
    • 您能否发布生成的 eml 文件内容作为对您问题的编辑? (在记事本中打开)。我在考虑 cid 的问题。
    • 不得不将其作为 HTML 添加到要点中,因为我必须删除机密信息
    • 你好像上传了word文档?这不是一个有效的 eml 文件。你能检查一下原始 eml,img 标签 cid 应该对应于消息中的多部分边界。
    猜你喜欢
    • 2013-05-08
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    相关资源
    最近更新 更多