图像未显示在电子邮件中(显示为.bin)

时间:2016-06-07 10:27:39

标签: c# asp.net email

我正在创建必须发送给技术人员的电子邮件。大约有12封电子邮件(根据选择的打印机型号,只会发送一封电子邮件)。电子邮件一切都很好。我遇到的唯一问题是图像没有显示,它们显示为带有.bin扩展名的附件。我正在使用案例陈述发送电子邮件。对我所做错的任何帮助都将不胜感激。感谢。

这就是我的代码的样子:

在案例陈述中:

    string headerPath = Server.MapPath("~\\Images\\EmailImages\\1022_03.png");
            LinkedResource img1022_03 = new LinkedResource(headerPath);
            img1022_03.ContentId = "1022_03";

            string tonerPath = Server.MapPath("~\\Images\\EmailImages\\TonerSignature.png");
            LinkedResource TonerSignature = new LinkedResource(tonerPath);
            TonerSignature.ContentId = "TonerSignature";

            string img1022_11Path = Server.MapPath("~\\Images\\EmailImages\\1022_11.png");
            LinkedResource img1022_11 = new LinkedResource(img1022_11Path);
            img1022_11.ContentId = "1022_11";

            AlternateView avDefault = AlternateView.CreateAlternateViewFromString(SendOMD(), null, MediaTypeNames.Text.Html);
            avDefault.LinkedResources.Add(img1022_03);
            avDefault.LinkedResources.Add(TonerSignature);
            avDefault.LinkedResources.Add(img1022_11);
            Mail.AlternateViews.Add(avDefault);

此代码位于默认的case语句中。在案例之外我添加了smtp详细信息:

     System.Configuration.Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
        MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
        if (mailSettings != null)
        {
            int port = mailSettings.Smtp.Network.Port;
            string host = mailSettings.Smtp.Network.Host;
            string password = mailSettings.Smtp.Network.Password;
            string username = mailSettings.Smtp.Network.UserName;
        }
        SmtpClient SMTP = new SmtpClient();
        SMTP.Send(Mail); 

这就是我的电子邮件方法:

    private string SendOMD()
    {
        string techType = radTechnicianType.SelectedValue.ToString();
        string techString = "";

        if (techType == "Field Technician")
            techString = "Please send a technician to replace the toner on the above mentioned machine.";
        else if (techType == "Onsite technician")
            techString = "Please replace the toner on the above mentioned machine.";
        else
            techString = "Please send a technician to replace the toner on the above mentioned machine.";

        StringBuilder stringBuilt = new StringBuilder();

        stringBuilt.AppendLine(@"<style type=""text/css"">");
        stringBuilt.AppendLine(".divStyle { font-family:Verdana,sans-serif, Arial, Helvetica; font-size:10pt; font-weight: bold; }");
        stringBuilt.AppendLine(".par { font-family:Verdana,sans-serif, Arial, Helvetica; font-size:10pt; width:658px; }");
        stringBuilt.AppendLine("table { border: 1px solid #000; border-collapse: collapse; font-family:Verdana; font-size:10pt; background-color:#dcdcdc; }");
        stringBuilt.AppendLine("table td { border-left: 1px solid #000; font-family:Verdana,sans-serif, Arial, Helvetica;  font-size:10pt; background-color:snow; border-bottom: 1px solid #000; padding-left:2px; margin-left:2px; padding-right:5px; margin-right:5px; }");
        stringBuilt.AppendLine("table td:first-child { border-left: none; font-family:Verdana,sans-serif, Arial, Helvetica;  font-size:10pt; background:#dcdcdc; background-color:#dcdcdc; border-bottom: 1px solid #000; }");
        stringBuilt.AppendLine("</style>");

        stringBuilt.AppendLine(@"<img alt="" src=""cid:1022_03"" width=""817"" height=""123"" />");
        stringBuilt.AppendLine("<br/>");

        stringBuilt.AppendLine(@"<p class=""par"">Dear " + txtClient.Text.Trim() + ",</p>");

        stringBuilt.AppendLine("Details:");
        stringBuilt.AppendLine("<br/>");

        stringBuilt.AppendLine("<table>");
        stringBuilt.AppendLine(@"<tr><td style=""background-color:#dcdcdc; widht:350px;"" >Model: </td><td style=""widht:250px;""> " + lstModelType.SelectedValue.ToString() + " </td></tr>");
        stringBuilt.AppendLine(@"<tr><td style=""background-color:#dcdcdc; widht:350px;"" >Serial No: </td><td style=""widht:250px;""> " + txtSerial.Text.Trim() + " </td></tr>");
        stringBuilt.AppendLine(@"<tr><td style=""background-color:#dcdcdc; widht:350px;"" >IP Address: </td><td style=""widht:250px;""> " + txtIPAddress.Text.Trim() + " </td></tr>");
        stringBuilt.AppendLine(@"<tr><td style=""background-color:#dcdcdc; widht:350px;"" >AD Sharename: </td><td style=""widht:250px;""> " + txtADSharename.Text.Trim() + " </td></tr>");
        stringBuilt.AppendLine(@"<tr><td style=""background-color:#dcdcdc; widht:350px;"">Department: </td><td style=""widht:250px;""> " + txtDepartment.Text.Trim() + " </td></tr>");
        stringBuilt.AppendLine(@"<tr><td style=""background-color:#dcdcdc; widht:350px;"">Colour: </td><td style=""widht:250px;""> " + lstColour.SelectedValue.ToString() + " </td></tr>");
        stringBuilt.AppendLine("</table>");

        stringBuilt.AppendLine(@"<p class=""par""><em>" + techString + "</em></p>");
        stringBuilt.AppendLine("<br/>");

        stringBuilt.AppendLine(@"<div class=""divStyle""> Regards, ");
        stringBuilt.AppendLine("<br/>");
        stringBuilt.AppendLine(lstApprover.SelectedValue.ToString());
        stringBuilt.AppendLine("</div>");
        stringBuilt.AppendLine(@"<img alt="" src=""cid:TonerSignature"" width=""300"" height=""70"" />");

        stringBuilt.AppendLine("<br/>");
        stringBuilt.AppendLine(@"<img alt="" src=""1022_11"" width=""815"" height=""54"" />");

        return stringBuilt.ToString();
    }

2 个答案:

答案 0 :(得分:1)

您需要将mime类型添加到资源中。

for ex:LinkedResource img1022_11 = new LinkedResource(img1022_11Path,MediaTypeNames.Image.Gif);

你可以添加Jpg,Tiff,Gif图像,对于png文件有一个解决方法。

使用它。

htmlView.LinkedResources.Add(
                      new LinkedResource("../EmailTemplate/ContactDetails.png")
                      {
                          ContentId = "ContactDetails",
                          TransferEncoding = TransferEncoding.Base64,
                          ContentType = new ContentType("image/png")
                      });

答案 1 :(得分:0)

而不是Server.MapPath使用网站路径来获取图像