Asp.Net Email Attachment Displaying Inline on Mobi

2019-09-04 05:20发布

I have an .Net 4.5 application that sends an email, with an attachment. It works as expected when the email is opened on a desktop, but when opened on a mobile (iPhone in this case) the attachment shows as inline HTML not as an attachment.

When however I forward the same email from my desktop to the phone, the attachment shows up correctly on my phone so I am almost certain that it has to do with how I am specifying mime or content-type, disposition etc. but I can't see what I am doing wrong.

Here is the code - note that

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

does create an attachment on iPhone but it is of type = mime-attachment that will not open.

I'm stumped & client awaits - any help greatly appreciated !

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

1条回答
Viruses.
2楼-- · 2019-09-04 05:56

Solved after some trial and error fiddling.

Counter-intuitively the attachment ContentDisposition object is READONLY which lead me to believe that I couldn't meddle in it however the read object is apparently a reference to the actual Attachment.ContentDisposition since setting values on the read instance does (apparently) correct the problem. Also used the Enum for MediaTypeNames (System.Net.Mime.MediaTypeNames.Text.Html) tho I don't think that was the issue.

Email send now looks like this :

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);
    }
查看更多
登录 后发表回答