GridView在创建发送邮件后出类拔萃c#

时间:2012-11-16 16:43:04

标签: c# excel gridview sendmail

我想发送一个.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);
            }

1 个答案:

答案 0 :(得分:0)

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