4

NPOI を使用して Excel ワークブックを作成し、それを電子メールの添付ファイルとして送信しようとしています。私のコードは次のとおりです。

var wb = new HSSFWorkbook();

//create the workbook

using(var ms = new MemoryStream())
{
    wb.Write(ms);
    var msg = new MailMessage();

    //create email

    msg.Attachments.Add(new Attachment(ms, "Document.xls", "application/vnd.ms-excel"));

    client.Send(msg);
}

ワークブックを作成するためのコードを除外しました。動作することをテストしました (ファイルを保存して問題なく開くことができます) が、何かを見たい場合はお問い合わせください。clientは私のものSmtpClientです。

電子メールは問題なく送信され、添付ファイルは Document.xls として表示されますが (予想どおり)、それを開くと次のメッセージが表示され (Excel 2010 の場合)、[はい] をクリックして開くと、ワークシートが空白になります。

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?

私の知る限りでは、形式を指定しています。誰かが私が間違っていることを見ていますか? どんな助けでも大歓迎です。

4

1 に答える 1