ABC
ABC

Reputation: 181

Error when sending Excel file as attachment

I am trying to create and save an excel file on server and later send it as a document. At the time of adding attachment, I am getting below error. Anyone have any idea, how can I resolve it ?

The process cannot access the file because it is being used by another process

Please find the code which I am using to create excel file and return path, that will be later added as document :

Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            string attachmentPath = Convert.ToString(ConfigurationManager.AppSettings["AttachmentPath"] as object);
            string path = attachmentPath + FileName;
            excel.Application.Workbooks.Add(true);

            int ColumnIndex = 0;
            foreach (DataColumn col in table.Columns)
            {
                ColumnIndex++;
                excel.Cells[1, ColumnIndex] = col.ColumnName;
            }
            int rowIndex = 0;
            foreach (DataRow row in table.Rows)
            {
                rowIndex++;
                ColumnIndex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    ColumnIndex++;
                    excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName].ToString();
                }
            }
            excel.DisplayAlerts = false;
            excel.ActiveWorkbook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,
            false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);                

            excel.Quit();
            return path + ".xls";

Below is the code where I use above path to add as an attachment to the email :

            if (attachmentPaths != null && attachmentPaths.Length > 0)
            {
                foreach (string path in attachmentPaths)
                {
                    if (!string.IsNullOrEmpty(path))
                        message.Attachments.Add(new System.Net.Mail.Attachment(path));
                }
            }
            SmtpClient smtp = new SmtpClient(objBO.SmtpDomain);
            smtp.Send(message);

Kindly provide suggestions. Thanks, in advance.

Upvotes: 1

Views: 1561

Answers (5)

ABC
ABC

Reputation: 181

Thank you all of you for your replies. But my problem is solved. Honestly, I don't know the exact reason, but what I did is just added below line after creating/saving excel file on server and before sending email attachment.

System.Threading.Thread.Sleep(1000);

Upvotes: 0

meda
meda

Reputation: 45490

In .net 4 attachments implements IDisposable, so you should make sure you have a using statement so that objects are disposed when out of scope.

     if (attachmentPaths != null && attachmentPaths.Length > 0)
            {
                foreach (string path in attachmentPaths)
                {
                    if (!string.IsNullOrEmpty(path))
                    {
                      using(Attachment data = new Attachment(path))
                       {
                            message.Attachments.Add(data);
                       }
                    }               
                }
            }

SmtpClient smtp = new SmtpClient(objBO.SmtpDomain);
smtp.Send(message);

Upvotes: 0

Kev
Kev

Reputation: 286

There are known issues with releasing excel com issues

See http://www.codeproject.com/Articles/9328/Release-Excel-Object

Upvotes: 0

ars265
ars265

Reputation: 1987

I don't know that this is the issue but you never Close your Workbook.

workbook.Close();
application.Quit();

Upvotes: 1

kbvishnu
kbvishnu

Reputation: 15630

This might because you may be opened the excel file from using MS Excel or you are any other programs to read the excel. Check your task manager and close all the applications and re-open the project and try again.

As per the latest comment, while you are doing like this

  1. create excel file
  2. send excel file
  3. delete excel file

I think you are trying to delete a file, which is sending . So please make sure that you start deleting the file only when the sending is complete.

Sample code

   SmtpClient smtp = new SmtpClient(objBO.SmtpDomain);
        try{
        smtp.Send(message);
        }
        catch{
        }
        finally{
         //deletes the file.
        }

Upvotes: 0

Related Questions