I have a bit of a strange use-case for System.Net.Mail in that I do not want to save physical files, at any point in my transaction.
I have a wcf web service that is acting as a router for mail. You make a simple request to send email and it works fine. You can also send it a string file path and it will pick up the file and send it properly.
What isn't working is a method that is trying to take a valid excel XML string (I know it's valid because if you use file writer to save it to a file, it opens as expected), convert it to a byte array and then send it to the web service which will create an attachment and add it to the MailMessage. When I get the attachment, it simply opens as an excel worksheet and each row is a line of the XML document.
This is the current output:
Row1: <?xml version="1.0"?>
Row2: <?mso-application progid="Excel.Sheet"?>
Row3: <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
Row4: xmlns:o="urn:schemas-microsoft-com:office:office"
Row5: xmlns:x="urn:schemas-microsoft-com:office:excel"
Row6: xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
Row7: xmlns:html="http://www.w3.org/TR/REC-html40">
Row8: <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
When I open up the file in Notepad++, I see that there are only LF elements at the end of the line, instead of what I would normally expect, LF and Carriage Returns.
Here is the code I have on the client side:
byte[] data = new byte[reportXml.Length * sizeof(char)];
System.Buffer.BlockCopy(reportXml.ToCharArray(), 0, data, 0, data.Length);
var result = emailClient.SendMailfromBinaryData(to, from, cc, subject, body, data, "attachment.xlsx");
Here is the code within the WCF service the adds the attachment to the MailMessage:
if (argFileStream != null && !string.IsNullOrEmpty(argFileName))
{
var stream = new MemoryStream(argFileStream);
var attachment = new Attachment(stream, argFileName, "text/plain");
attachment.NameEncoding = new UTF8Encoding();
message.Attachments.Add(attachment);
}
Not that I also tried to change the MIME type to excel:
var attachment = new Attachment(stream, argFileName, "application/vnd.ms-excel");
Any ideas? I'm not getting any errors, it's just that excel simply won't recognize that the XML is actually an excel file as XML.