Search code examples
c#.netasp.net-mvcmemorystream.doc

How to attach .docx and .doc files to the email from memory stream?


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...

Solution

  • 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