【问题标题】:how to attach the programmatically created EXCEL to SMTP MAIL in the application?如何将程序创建的 EXCEL 附加到应用程序中的 SMTP MAIL?
【发布时间】:2013-07-06 12:26:00
【问题描述】:

我需要以编程方式创建的 EXCEL 附加到应用程序中的 SMTP MAIL ....我已使用一种方法将数据表转换为 EXCEL,该方法在我运行此应用程序时下载 EXCEL 文件,但我的要求是将创建的文件附加到我的 smtp...我不知道如何从该方法返回创建的 excel 文件...请任何人帮助我吗?

这是将数据表转换为 EXCEL 的代码:

public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{
 context.Response.Write(column.ColumnName + ";");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
 for (int i = 0; i < table.Columns.Count; i++)
 {
  context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
 }
 context.Response.Write(Environment.NewLine);
 }
 context.Response.ContentType = "text/csv";
 context.Response.AppendHeader
 ("Content-Disposition", "attachment; filename=" + name +".csv");
  context.Response.End();
 }

【问题讨论】:

    标签: asp.net excel class methods email-attachments


    【解决方案1】:

    以下是一些可能有所帮助的重构代码:

    private static Stream DataTableToStream(DataTable table)
    {
        const string semiColon = ";";
    
        var ms = new MemoryStream();
        var sw = new StreamWriter(ms);
    
        foreach (DataColumn column in table.Columns)
        {
            sw.Write(column.ColumnName);
            sw.Write(semiColon);
        }
        sw.Write(Environment.NewLine);
        foreach (DataRow row in table.Rows)
        {
            for (int i = 0; i < table.Columns.Count; i++)
            {
                sw.Write(row[i].ToString().Replace(semiColon, string.Empty));
                sw.Write(semiColon);
            }
            sw.Write(Environment.NewLine);
        }
        return ms;
    }
    
    private static MailMessage CreateMail(string from, 
        string to, 
        string subject, 
        string body, 
        string attname,  
        Stream tableStream)
    {
        // using System.Net.Mail
        var mailMsg = new MailMessage(from, to, subject, body);
    
        tableStream.Position = 0;
        mailMsg.Attachments.Add(
            new Attachment(tableStream, attname, CsvContentType));
        return mailMsg;
    }
    
    private const string CsvContentType= "text/csv";
    
    private static void ExportToSpreadsheetInternal(Stream tableStream, string name)
    {
    
        HttpContext context = HttpContext.Current;
        context.Response.Clear();
        context.Response.ContentType = CsvContentType ;
        context.Response.AppendHeader(
            "Content-Disposition"
            , "attachment; filename=" + name + ".csv");
    
        tableStream.Position = 0;
        tableStream.CopyTo(context.Response.OutputStream);
    
        context.Response.End();
    
    }
    
    public static void ExportToSpreadsheet(DataTable table, string name)
    {
        var stream = DataTableToStream(table);
        var mailMsg = CreateMail("from@example.com", 
            "to@example.com", 
            "spread", 
            "the spread", 
            name, 
            stream);
        ExportToSpreadsheetInternal(stream, name);
       // send the mailMsg with SmtpClient (config in your web.config)
       var smtp = new SmtpClient();
       smtp.Send(mailMsg);
    }
    

    将此添加到您的 web.config,假设您使用 gmail,请将 userpwd 替换为您的...

      <system.net>
        <mailSettings>
            <smtp deliveryMethod="network" from="you@example.com">
              <network 
                host="smtp.gmail.com" 
                port="587"
                enableSsl="true"
                userName="user" 
                password="pwd"/>
            </smtp>
        </mailSettings>
      </system.net>
    

    【讨论】:

    • 嗨 rene,谢谢您的宝贵时间。请您解释一下如何?因为我是 ASP.NET 的新手...请。
    • 代码是替换掉的,所以你用我的替换你的行。为了让电子邮件继续运行,还将我刚刚编辑的内容添加到 web.config。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    相关资源
    最近更新 更多