C#使用附件发送电子邮件(图像)

Chr*_*Run 5 c# asp.net smtp email-attachments

我的方法使用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;
            }
        }
Run Code Online (Sandbox Code Playgroud)
  1. uploadedImage不为null.
  2. ContentLength是1038946字节(正确的大小).

但是,正在发送的电子邮件包含图像作为具有正确文件名的附件,尽管它的大小为0字节.

我错过了什么?

Tro*_*ers 1

的构造函数的第二个参数System.Net.Mail.Attachment不是文件名。这是内容类型。也许在创建附件之前确保您的流位置为 0