Asp.Net电子邮件附件显示内嵌于手机(iPhone) [英] Asp.Net Email Attachment Displaying Inline on Mobile (iPhone)
问题描述
我有一个.NET 4.5的应用程序,发送电子邮件,带有附件。它的工作原理的电子邮件被打开一个桌面上时如预期的,但(在这种情况下,iPhone上)的移动打开时,附件显示为内嵌HTML不是作为附件。
在,但是我转发来自我的桌面到手机相同的电子邮件,附件正确地显示了我的手机上,所以我几乎可以肯定,它与我是如何指定MIME或内容类型,处置等做,但我看不出有什么我做错了。
下面是code - 注意,
att.ContentType =新System.Net.Mime.ContentType(多部分/混合);
确实创建iPhone的附件,但它是类型= MIME-附件不会打开。
我难倒&安培;客户等待 - !任何帮助非常AP preciated
私人无效SendNotice(绳体,字符串连接,字符串email,布尔PDF = FALSE)
{
消息MAILMESSAGE新= MAILMESSAGE();
message.From =新的MailAddress(ConfigurationManager.AppSettings [SMTP.SendFrom]);
message.Subject = ConfigurationManager.AppSettings [MatchedNoticeSubject];
message.To.Add(新MailAddress(电子邮件));
message.ReplyToList.Add(新MailAddress(ConfigurationManager.AppSettings [SMTP.ReplyTo]));
message.Body =身体;
message.IsBodyHtml = TRUE; 附件ATT = Attachment.CreateAttachmentFromString(附件,SeniorInfo.html,System.Text.Encoding.ASCII,text / html的); //指定此创建类型不开的mime附件的一个附件
//att.ContentType =新System.Net.Mime.ContentType(多部分/混合); message.Attachments.Add(ATT);
SmtpClient服务器=新SmtpClient()
{
EnableSsl =(ConfigurationManager.AppSettings [SMTP.EnableSSL]。ToLower将()==真),
主机= ConfigurationManager.AppSettings [SMTP.Server],
端口= Convert.ToInt16(ConfigurationManager.AppSettings [SMTP.Port])
证书=新System.Net.NetworkCredential(ConfigurationManager.AppSettings [SMTP.Account],ConfigurationManager.AppSettings [SMTP.Password])
};
server.Send(消息);
}
在一些试验和错误摆弄解决。
反直觉附件ContentDisposition对象是只读这使我相信,我不能在被扎不过读取的对象显然是因为在读的实例设定值与实际Attachment.ContentDisposition参考做(显然)解决这个问题。还使用了枚举的MediaTypeNames(System.Net.Mime.MediaTypeNames.Text.Html)尽管我不认为这是问题。
现在邮件发送看起来是这样的:
私人无效SendMatchNotice(绳体,字符串连接,字符串email,布尔PDF = FALSE)
{
消息MAILMESSAGE新= MAILMESSAGE();
message.From =新的MailAddress(ConfigurationManager.AppSettings [SMTP.SendFrom]);
message.Subject = ConfigurationManager.AppSettings [MatchedNoticeSubject];
message.To.Add(新MailAddress(电子邮件));
message.ReplyToList.Add(新MailAddress(ConfigurationManager.AppSettings [SMTP.ReplyTo]));
message.Body =身体;
message.IsBodyHtml = TRUE;
//创建此电子邮件文件附件。
附件ATT = Attachment.CreateAttachmentFromString(附件,SeniorInfo.html,System.Text.Encoding.ASCII,System.Net.Mime.MediaTypeNames.Text.Html);
System.Net.Mime.ContentDisposition处置= att.ContentDisposition;
disposition.DispositionType =附件;
disposition.Inline = FALSE;
disposition.FileName =SeniorInfo.html;
disposition.CreationDate = DateTime.Now;
disposition.ModificationDate = DateTime.Now;
disposition.ReadDate = DateTime.Now;
message.Attachments.Add(ATT);
SmtpClient服务器=新SmtpClient()
{
EnableSsl =(ConfigurationManager.AppSettings [SMTP.EnableSSL]。ToLower将()==真),
主机= ConfigurationManager.AppSettings [SMTP.Server],
端口= Convert.ToInt16(ConfigurationManager.AppSettings [SMTP.Port])
证书=新System.Net.NetworkCredential(ConfigurationManager.AppSettings [SMTP.Account],ConfigurationManager.AppSettings [SMTP.Password])
};
server.Send(消息);
}
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);
}
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);
}
这篇关于Asp.Net电子邮件附件显示内嵌于手机(iPhone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!