I use NPOI to create an Excel workbook and try to send it as an attachment in an email. My code is as follows:
var wb = new HSSFWorkbook();
using(var ms = new MemoryStream())
{
wb.Write(ms);
var msg = new MailMessage();
msg.Attachments.Add(new Attachment(ms, "Document.xls", "application/vnd.ms-excel"));
client.Send(msg);
}
I excluded the code for creating the book, I tested it to make sure that it works (I can save the file and open it without problems), but if you want to see something, ask. client- it's just mine SmtpClient.
The letter is sent without problems, and the attachment is present as Document.xls (as expected), but when I open it, I get the following message (in Excel 2010), and when I click "Yes" to open, the worksheet is empty.
The file you are trying to open, 'Document.xls', is in a differrent format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
As far as I know, I indicate the format. Does anyone see what I'm doing wrong? Any help would be appreciated.