我想发送一个.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);
}
答案 0 :(得分:0)
您可能需要指明附件的类型