我的方法使用 SMTP 中继服务器发送电子邮件。
一切正常(电子邮件已发送),除了附件(图像)以某种方式压缩/不存在并且无法从电子邮件中检索之外。
该方法如下所示:
public static bool SendEmail(HttpPostedFileBase uploadedImage)
{
try
{
var message = new MailMessage() //To/From address
{
Subject = "This is subject."
Body = "This is text."
};
if (uploadedImage != null && uploadedImage.ContentLength > 0)
{
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(uploadedImage.InputStream, uploadedImage.FileName);
message.Attachments.Add(attachment);
}
message.IsBodyHtml = true;
var smtpClient = new SmtpClient();
//SMTP Credentials
smtpClient.Send(message);
return true;
}
catch (Exception ex)
{
//Logg exception
return false;
}
}
- 上传的图片不为空。
- ContentLength 为 1038946 字节(正确大小)。
但是,正在发送的电子邮件包含具有正确文件名的附件图像,尽管其大小为 0 字节。
我缺少什么?
构造函数的第二个参数System.Net.Mail.Attachment
不是文件名。这是内容类型 https://msdn.microsoft.com/en-us/library/6sdktyws%28v=vs.110%29.aspx。
也许在创建附件之前确保您的流位置为 0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)