2014-10-17 52 views
0

我有一個.Net 4.5應用程序發送電子郵件,附件。在電子郵件在桌面上打開時,它會按預期工作,但在移動設備上打開時(本例中爲iPhone),附件將顯示爲內嵌HTML,而不是附件。Asp.Net電子郵件附件在手機上顯示內嵌(iPhone)

但是,當我將同一封電子郵件從我的桌面轉發到手機時,附件在我的手機上正確顯示,所以我幾乎可以確定它與我如何指定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); 
    } 

回答

0

經過一些試驗和錯誤擺弄解決。

與直覺相反,附件ContentDisposition對象是READONLY,它使我相信我不能插手它,但讀取對象顯然是對實際Attachment.ContentDisposition的引用,因爲在讀取實例上設置值會(顯然)糾正了這個問題。還使用Enum for 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); 
    }