使用Interop.Excel发送电子邮件而不保存

时间:2017-06-12 19:36:51

标签: c# excel email interop

我正在尝试每周特定时间发送电子邮件。 但是当我使用这段代码时,它一直在询问

“此位置已存在名为”C:\ Windows \ Temp \ employee.xls“的文件。是否要替换它?”

有没有办法直接发送电子邮件而不保存那些xls文件?

这是我的代码。

    private void SendEmail()
    {
        //get the data from database
        DataTable data = GetData();
        DataTable email_data = GetEmailData();


        // Create an Excel object and add workbook...
        Excel.ApplicationClass excel = new Excel.ApplicationClass();
        Excel.Workbook workbook = excel.Application.Workbooks.Add(true); // true for object template???

        // Global missing reference for objects we are not defining...
        object missing = System.Reflection.Missing.Value;

        // Add column headings...
        int iCol = 0;
        foreach (DataColumn c in data.Columns)
        {
            iCol++;
            excel.Cells[1, iCol] = c.ColumnName;
        }
        // for each row of data...
        int iRow = 0;
        foreach (DataRow r in data.Rows)
        {
            iRow++;

            // add each row's cell data...
            iCol = 0;
            foreach (DataColumn c in data.Columns)
            {
                iCol++;
                excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
            }
        }



        // If wanting to Save the workbook...


        workbook.SaveAs(@"C:\Windows\Temp\employee.xls",
        Excel.XlFileFormat.xlWorkbookNormal, missing, missing,
        false, false, Excel.XlSaveAsAccessMode.xlExclusive,
        missing, missing, missing, missing, missing);


        workbook.Close();

        String from = "aa@gmail.com";

        String to = "bb@gmail.com";

        using (MailMessage mm = new MailMessage(from, to))
        {
       SmtpClient smtp = new SmtpClient();
            mm.Subject = "List";
            mm.Attachments.Add(new Attachment(@"C:\Windows\Temp\employee.xls"));
            mm.IsBodyHtml = true;

            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
            credentials.UserName = "aa@gmail.com";
            credentials.Password = "1234";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = credentials;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }

1 个答案:

答案 0 :(得分:0)

Workbook.SendMail方法,according to documentation

  

将活动工作簿发送给单个收件人

并且之前无需调用workbook.SaveAs(邮件客户端可能仍会根据自己的判断在磁盘上的某处创建附件的临时文件,这是另一回事。)

第二种可能性:使用像EPPlus这样的开放文档格式库(对于您在代码中执行的任务,不需要重量级Excel自动化),因为您可以通过流式传输绕过文件系统该文件直接进入附件:

https://msdn.microsoft.com/en-us/library/ab7hb4y5(v=vs.110).aspx

using (var package = new ExcelPackage())
{
    var worksheet = package.Workbook.Worksheets.Add("sheet");

    /* fill sheet with data 
     * ... */

    /* compose mail 
     * ... */

    var contenttype = new ContentType("application/vnd.ms-excel");
    var stream = new MemoryStream(package.GetAsByteArray());
    mm.Attachments.Add(new Attachment(stream, contenttype);

    /* send mail */
}

修改

考虑到评论:我将您的问题解释为更多"如何摆脱提示"而不是"我可能不会在文件系统中写任何东西",所以在保存工作簿之前以及发送邮件之后,你可能最好这样做:

var filename = @"C:\Windows\Temp\employee.xls";
if(File.Exists(filename)  
{
    File.Delete(filename);
}

如果这是您计划分发的软件,则需要使用GetTempPath替换该硬编码路径。