Asp.Net电子邮件附件在手机(iPhone)上显示内联

本文关键字:iPhone 显示 手机 电子邮件 Net Asp | 更新日期: 2023-09-27 18:27:00

我有一个.Net 4.5应用程序,它会发送一封带有附件的电子邮件。当在桌面上打开电子邮件时,它可以正常工作,但当在手机(本例中为iPhone)上打开时,附件显示为内联HTML,而不是附件。

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

这是代码-注意

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

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

我被难住了&客户等待-非常感谢任何帮助!

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);
    }

Asp.Net电子邮件附件在手机(iPhone)上显示内联

经过一些尝试和错误处理后解决。

与直觉相反,附件ContentDisposition对象是READONLY,这让我相信我不能干预它,但是读取对象显然是对实际attachment.ContentDisposition的引用,因为在读取实例上设置值确实(显然)纠正了问题。还使用了MediaTypeNames的Enum(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);
    }