C# Sending Email with attached file (image)

烂漫一生 提交于 2020-01-03 12:46:07

问题


My method sends an email using a SMTP Relay Server.

Everything works fine (the email gets sent), except for that the attached file (the image) is somehow compressed/notexistent and not able to retrieve from the email.

The method looks like this:

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;
            }
        }
  1. The uploadedImage is not null.
  2. ContentLength is 1038946 bytes (correct size).

However, the email that is being sent contains the image as an attachment with correct filename, although it's size is 0 bytes.

What am I missing?


回答1:


The second parameter of constructor of System.Net.Mail.Attachment is not the file name. It's the content type. And perhaps ensure your stream position is 0 before to create attachment




回答2:


@ChrisRun,

  1. You should change the parameter HttpPostedFileBase as byte[] for example. This way you could re-use your class in more places.
  2. Try changing FileName for ContentType and add the MediaTypeNames.Image.Jpeg.
  3. Also, add the using directive for dispose the MailMessage and SmtpClient

        using (var message = new MailMessage
        {
            From = new MailAddress("from@gmail.com"),
            Subject = "This is subject.",
            Body = "This is text.",
            IsBodyHtml = true,
            To = { "to@someDomain.com" }
        })
        {
            if (imageFile != null && imageFile.ContentLength > 0)
            {
                message.Attachments.Add(new Attachment(imageFile.InputStream, imageFile.ContentType, MediaTypeNames.Image.Jpeg));
            }
    
            using (var client = new SmtpClient("smtp.gmail.com")
            {
                Credentials = new System.Net.NetworkCredential("user", "password"),
                EnableSsl = true
            })
            {
                client.Send(message);
            }
        }
    

Cheers



来源:https://stackoverflow.com/questions/34787198/c-sharp-sending-email-with-attached-file-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!