MailMessage - 打开Excel附件时出错

时间:2014-07-24 08:37:08

标签: c# asp.net excel

public void SendMail(string smtpAddress, string subject, string body, bool isBodyHtml,    string from, string to, string cc, string bcc,DataTable dt)
        {
           SmtpClient smtpClient = null;
        try
        {
            smtpClient = new SmtpClient();
            smtpClient.Host = smtpAddress;
            MailMessage mailMessage = new MailMessage(from, to, subject, body);
            mailMessage.BodyEncoding = Encoding.UTF8;
            mailMessage.IsBodyHtml = true;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                Byte[] contentAsBytes = SerializeData(dt);
                memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);
                memoryStream.Position = 0;
                Attachment attachment = new Attachment(memoryStream, "test.xls", "application/vnd.ms-excel");   
                mailMessage.Attachments.Add(attachment);
                smtpClient.Send(mailMessage);
                memoryStream.Close();
            }
        }
        catch (Exception)
        {

        }

    }

MailMessage已生成但附加了excel,表示您尝试打开的文件采用的格式与扩展名指定的格式不同。在打开文件之前,验证文件是否已损坏且是否受信任来源信任。在打开xsl时,它会生成xsd格式 请帮助

0 个答案:

没有答案