0

我想发送一个 .xlsx ,首先我创建(它有 html 代码)然后我使用 SMTP 服务器发送它,它确实附加了文件但是当我试图打开它时“它说文件已损坏等“有什么帮助吗?

这是我的代码

    try
            {

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            // Render grid view control.
            gvStock.RenderControl(htw);

            // Write the rendered content to a file.
            string renderedGridView = sw.ToString();
            File.WriteAllText(@"C:\test\ExportedFile.xls", renderedGridView);

           // File.WriteAllText(@"C:\test\ExportedFile.xls", p1);
            }
            catch (Exception e) 
            {
            Response.Write(e.Message);
            }


            try
            {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("server");
            mail.From = new MailAddress("no-reply@mail.com");
            mail.To.Add("db@gmail.com");
            mail.Subject = "Test Mail - 1";
            mail.Body = "mail with attachment";



            Attachment data = new Attachment("C:/test/ExportedFile.xls");
            mail.Attachments.Add(data);

            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential("user", "pass");
          //  SmtpServer.EnableSsl = true;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Send(mail);
            }
            catch( Exception e) 
            {
            Response.Write(e.Message);
            }
4

1 回答 1

0

您可能需要指明附件的类型

于 2012-11-16T17:04:31.013 回答