【问题标题】:Asp.Net Email Attachment Displaying Inline on Mobile (iPhone)Asp.Net 电子邮件附件在移动设备 (iPhone) 上显示内联
【发布时间】:2014-10-17 03:40:22
【问题描述】:

我有一个 .Net 4.5 应用程序,它发送带有附件的电子邮件。当电子邮件在桌面上打开时,它按预期工作,但在移动设备(本例中为 iPhone)上打开时,附件显示为内联 HTML 而不是附件。

但是,当我将同一封电子邮件从我的桌面转发到手机时,附件会正确显示在我的手机上,所以我几乎可以肯定它与我如何指定 mime 或内容类型、处置等有关。但是我看不出我做错了什么。

这是代码 - 请注意

att.ContentType = new System.Net.Mime.ContentType("multipart/mixed");

确实在 iPhone 上创建了一个附件,但它的类型 = mime-attachment 不会打开。

我很困惑,客户正在等待 - 非常感谢任何帮助!

private void SendNotice(string body, string attachment, string email, bool pdf = false)
    {
        MailMessage message = new MailMessage();
        message.From = new MailAddress(ConfigurationManager.AppSettings["SMTP.SendFrom"]);
        message.Subject = ConfigurationManager.AppSettings["MatchedNoticeSubject"];
        message.To.Add(new MailAddress(email));
        message.ReplyToList.Add(new MailAddress(ConfigurationManager.AppSettings["SMTP.ReplyTo"]));
        message.Body = body;
        message.IsBodyHtml = true;

        Attachment att = Attachment.CreateAttachmentFromString(attachment, "SeniorInfo.html", System.Text.Encoding.ASCII, "text/html");

        //specifying this creates an attachment of type "mime-attachment" that does not open
        //att.ContentType = new System.Net.Mime.ContentType("multipart/mixed");

        message.Attachments.Add(att);
        SmtpClient server = new SmtpClient()
        {
            EnableSsl = (ConfigurationManager.AppSettings["SMTP.EnableSSL"].ToLower() == "true"),
            Host = ConfigurationManager.AppSettings["SMTP.Server"],
            Port = Convert.ToInt16(ConfigurationManager.AppSettings["SMTP.Port"]),
            Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTP.Account"], ConfigurationManager.AppSettings["SMTP.Password"])
        };
        server.Send(message);
    }

【问题讨论】:

    标签: c# asp.net mobile email-attachments


    【解决方案1】:

    经过反复试验后得到解决。

    与直觉相反,附件 ContentDisposition 对象是只读的,这使我相信我无法干预它,但是读取的对象显然是对实际 Attachment.ContentDisposition 的引用,因为在读取实例上设置值确实(显然)纠正问题。还使用了 MediaTypeNames 的枚举 (System.Net.Mime.MediaTypeNames.Text.Html),但我认为这不是问题。

    电子邮件发送现在看起来像这样:

    private void SendMatchNotice(string body, string attachment, string email, bool pdf = false)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(ConfigurationManager.AppSettings["SMTP.SendFrom"]);
            message.Subject = ConfigurationManager.AppSettings["MatchedNoticeSubject"];
            message.To.Add(new MailAddress(email));
            message.ReplyToList.Add(new MailAddress(ConfigurationManager.AppSettings["SMTP.ReplyTo"]));
            message.Body = body;
            message.IsBodyHtml = true;
            // Create  the file attachment for this e-mail message.
            Attachment att = Attachment.CreateAttachmentFromString(attachment, "SeniorInfo.html", System.Text.Encoding.ASCII, System.Net.Mime.MediaTypeNames.Text.Html);
            System.Net.Mime.ContentDisposition disposition = att.ContentDisposition;
            disposition.DispositionType = "attachment";
            disposition.Inline = false;
            disposition.FileName = "SeniorInfo.html";
            disposition.CreationDate = DateTime.Now;
            disposition.ModificationDate = DateTime.Now;
            disposition.ReadDate = DateTime.Now;
            message.Attachments.Add(att);
            SmtpClient server = new SmtpClient()
            {
                EnableSsl = (ConfigurationManager.AppSettings["SMTP.EnableSSL"].ToLower() == "true"),
                Host = ConfigurationManager.AppSettings["SMTP.Server"],
                Port = Convert.ToInt16(ConfigurationManager.AppSettings["SMTP.Port"]),
                Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTP.Account"], ConfigurationManager.AppSettings["SMTP.Password"])
            };
            server.Send(message);
        }
    

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多