14693

Asp.Net Email Attachment Displaying Inline on Mobile (iPhone)

Question:

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

Answer1:

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

Recommend

  • Difficulty with Simple Regex (match prefix/suffix)
  • Any way of appending to the act method in scala?
  • XPathResultType definition error?
  • Does Cocoa connection binding to NSToolbarItem prevent deinitializing?
  • Deleting a widget from QTableView
  • How to display the images in listview
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Get the number 18437736874454810627
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • Creating PDF from TIFF image using iText
  • Application level floating views with navigation in Android
  • Row to Column conversion in Talend
  • jQuery Orbit - How to make a Random Slideshow?
  • Build Successful but not running on simulator
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Custom Tabgroup Appcelerator
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Projection media query: browser support and workarounds?
  • Different response to non-authenticated users and AJAX calls
  • output of program is not same as passed argument
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Javascript + PHP Encryption with pidCrypt
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Getting Messege Twice Using IMvxMessenger
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?