Click here to Skip to main content
15,909,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have encountered this problem and trying to add Dispose() function but still i get this error:

System.IO.IOException: The process cannot access the file 'c:\upload\07 - 2011.doc' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at newsletter.SendmailToAll()



And here is the code:

protected void SendmailToAll()
    {
        string sql = "select email from distributors where email <> '' and var1='1'";
        oCon = oDB.dbOpen();
        SqlCommand DBCommand = new SqlCommand(sql, oCon);
        SqlDataReader sdr = DBCommand.ExecuteReader();
        while (sdr.Read())
        {
            email_id.Add(sdr["email"].ToString());
        }
        sdr.Close();
        oDB.dbClose();


        try
        {

            System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage();

            if (FileUpload1.PostedFile.FileName != "")
            {
                string strdir = ConfigurationManager.AppSettings["MailAttachementPath"];
                string strfilename = Path.GetFileName(FileUpload1.PostedFile.FileName);

                FileUpload1.PostedFile.SaveAs(strdir + strfilename);

                msgMail.Attachments.Add(new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, strfilename));
            }


            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.com");
            smtp.Credentials = new NetworkCredential("test@mail.com", "pass");
            
            string strBody = "[message]";
            strBody = strBody.Replace("[message]", txtmsgBox.Text.ToString());
            //string mailto = "";
            for (int i = 0; i < email_id.Count; i++)
            {
                string email = email_id[i].ToString();

                if (email.Contains("@") && email.Contains("."))
                {
                    System.Threading.Thread.Sleep(60);
                    msgMail.To.Clear();
                    msgMail.To.Add(email);
                    msgMail.From = new System.Net.Mail.MailAddress("test@mail.com");
                    msgMail.Subject = "Newsletter";
                    msgMail.IsBodyHtml = true;

                    msgMail.Body = strBody;

                    smtp.Send(msgMail);

                    lblmsg.Text = "";
                    email = "";
                   

                    
                }

            }           //Here I Add Dispose function.
                        msgMail.Dispose();
                        
            if (lblmsg.Text == "")
            {
                lblmsg.Text = "Newsletter has been sent Successfully!";
                txtmsgBox.Text = "";
                txtEmail.Text = "";
            }
            
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.ToString();
        }
        
    }



Thank you.
Posted

Rather than doing it that way, why not use the BCC list[^]? That way, you just send it to a "safe" main address, and all the others get a copy without see the names. Single email leaves your system, arrives in all recipients. Then the file will not be in use for anyone...
 
Share this answer
 
Comments
riconalla 7-Jul-11 10:28am    
Thank you but when I send an email(s) i only have an option to check the checkbox if I will send it to everyone and type only the email address and the error goes when I send it to everyone.
someone might have the file open for reading.....so you cannot exclusively touch it.
So check if file exists and can be opened exclusive
<pre lang="C++">public static FileExist DoesFileExistOnServer(String FolderAndFileName)
        {
            // this class does not work
            FileExist doesExist = FileExist.No;
            if (!String.IsNullOrEmpty(FolderAndFileName))
            {
                FileInfo fi = new FileInfo(FolderAndFileName);
                if (fi.Exists)
                {
                    FileStream stream = null;
                    try
                    {
                        // check if the file can be opened exclusive. If that is possible, then the file is found otherwise in transfer
                        using (stream = File.Open(FolderAndFileName, FileMode.Open, FileAccess.Write, FileShare.None))
                        {
                            if (stream.CanWrite)
                                doesExist = FileExist.Yes;
                            else
                                doesExist = FileExist.Transferring;
                        }
                    }
                    catch (Exception err)
                    {
                        
                        doesExist = FileExist.Transferring;
                    }
                }
            }
            

            return doesExist;
        }
 
Share this answer
 
Comments
riconalla 7-Jul-11 10:30am    
Can you point out where part of my code that needs to be changed or add some code to fix the error? Thanks.
Herman<T>.Instance 12-Jul-11 4:01am    
before you do
FileUpload1.PostedFile.SaveAs(strdir + strfilename);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900