我的方法使用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;
}
}
- uploadedImage 不为空。
- ContentLength 为 1038946 字节(正确大小)。
然而,发送的电子邮件包含正确文件名的图像作为附件,尽管其大小为 0 字节。
我错了什么?