0

I'm doing the file attachment through memory stream because temporary storage is not an option.

Here I did a Jpeg image attachment. I looked at other file types with which you could do the same by switching the MediaTypeNames, and unfortunately .doc and .docx is not among them.

I was wondering whether any of you know of any package and how to use it for this particular occasion?

//...set up MailMessage, add a bunch of non-file content to it

MemoryStream jpgStream = new MemoryStream();
string filename = uploadedFile.FileName;

System.Drawing.Image theImage = System.Drawing.Image.FromStream(uploadedFile.InputStream);

theImage.Save(jpgStream, System.Drawing.Imaging.ImageFormat.Jpeg); 

jpgStream.Seek(0L, SeekOrigin.Begin);
emailMessage.Attachments.Add(new Attachment(jpgStream, filename, System.Net.Mime.MediaTypeNames.Image.Jpeg));

//something extra and send email...

2 Answers2

0

You should use MediaTypeNames.Application.Octet as mime type.

From : https://learn.microsoft.com/fr-fr/dotnet/api/system.net.mime.mediatypenames.application.octet?view=netframework-4.7.2

Benoit Gidon
  • 79
  • 10
  • If I do that, then the attached file will be impossible to read. Reason being I haven't encoded the byte stream into a file. For instance, this line: ``theImage.Save(jpgStream, System.Drawing.Imaging.ImageFormat.Jpeg); `` encodes the stream into ``jpeg`` and only then the attachment becomes readable. Unfortunately, I can't use it for ``.docx`` because it'ss not an image and this ``Save()`` method is from ``System.Drawing.Image`` namespace. – Liudvikas Taluntis Feb 08 '19 at 15:22
  • 1
    Omg >_< I jumped to conclusions here, I'm ``sorry to have ever doubted you`` :D. I did try this before with a ``different image formats`` (like ``.png`` and even the same ``.jpg``) and it didn't work. Now I tried whatever you said just in case, and it seemed to work fine with ``.docx`` I'll do some more tests with more complex content inside the ``.docx`` file just in case :) – Liudvikas Taluntis Feb 08 '19 at 15:37
0

Thanks Benoit Gidon for his answer. I was proved once again that your assumptions are your worst enemy.

Apparently it's as simple as that and you don't need any other special methods as long as you put the file.InputStream in directly into attachment, unlike other S.O. posts make you believe: https://stackoverflow.com/questions/9095880/attach-multiple-image-formats-not-just-jpg-to-email

https://stackoverflow.com/questions/5336239/attach-a-file-from-memorystream-to-a-mailmessage-in-c-sharp

In case anyone is actually struggling with it, here is the code:

foreach (HttpPostedFileBase file in fullViewModel.filesCollection)
            {
                string filename = file.FileName;

                 msg.Attachments.Add(new Attachment(file.InputStream, filename, MediaTypeNames.Application.Octet));
            }

I tested this with a .docx document that had 5000 words of content in it, as well as tables and pictures, and it gets reconstructed the way it was in my gmail client.

Just tested that, it also works the same way with .png, so no need for PngBitmapDecoder.