How to send email with attachment of self-defined file extension name through Amazon?

StackOverflow https://stackoverflow.com/questions/19779539

  •  03-07-2022
  •  | 
  •  

Question

I'm trying to send an email with attachment of self-defined file extension name, like 'file.aaa'. I don't know how to set it's MIME content type. I new an atachment as 'new Attachement(stream, null, null)'. But then got an Amazon exception of 'Illegal file name file.aaa'.

Was it helpful?

Solution

You must specify a MIME type in email. You can use application/octet-stream to specify it should be handled as a binary file, text/plain for a text file or you can make your as application/vnd.yourcompany.yourapplicationname - anything that starts with type/vnd.company are reserved for private use. See the Wikipedia article for more info.


Apparently, Amazon restricts both file extensions and MIME types. I would consider zipping your custom format as apparently that is allowed. See the developer documentation for more.

var inputfile = "foobar.aaa";
var tempdir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var outZipFile = File.GetTempFileName();

File.Move(inputfile, tempdir);
ZipFile.CreateFromDirectory(tempdir, outZipFile);
Directory.Delete(tempdir, true /* delete the file within */);

var attachment = new Attachment(outZipFile, "yourFile.zip", "application/octet-stream");
// send your email
File.Delete(outZipFile);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top