MailMessage - 打开附件时出错

 kg9854997 发布于 2023-02-11 11:12

我正在使用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?

据我所知,我正在指定格式.有谁看到我做错了什么?任何帮助,将不胜感激.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有