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 = "";
}
}
msgMail.Dispose();
if (lblmsg.Text == "")
{
lblmsg.Text = "Newsletter has been sent Successfully!";
txtmsgBox.Text = "";
txtEmail.Text = "";
}
}
catch (Exception ex)
{
lblmsg.Text = ex.ToString();
}
}
1. This file is used in other process. (May file is in open mode) Close the file before debugging if it is open. Or some other application using the file.
2. You don't have the permissons to access the file. check file permission by right click on folder, go to security tab, select the user(......) options, now click the edit button , check the full control check box.
Code is important, but it's a small part of the overall process.
Thanks I follow what you told me but I got the same error when I try to send it again. Can you point out on where part of my code that needs to changed or add new code to make it work?
Why to loop through all email addresses and send mail 1 by 1. Just loop through Email addresses, generate an Email address string where in each email address is separated by semicolon i.e. ';' and just pass the same to the 'To' attribute of the MailMessage
object. Thats it.
Because it sends the mail in loop after the first iteration gets over, it doesn't release the file i.e. it still remains in use and then in the second iteration it starts to use the same file.
Try this and let me know what happened!
Mark this as Answer if it helps!
multiple
Aashish Surani (Synoverge Technologies Pvt. Ltd.) Mark as Answer if it helped!!! :)
Why to loop through all email addresses and send mail 1 by 1. Just loop through Email addresses, generate an Email address string where in each email address is separated by semicolon i.e. ';' and just pass the same to the 'To' attribute of the MailMessage
object. Thats it.
Because it sends the mail in loop after the first iteration gets over, it doesn't release the file i.e. it still remains in use and then in the second iteration it starts to use the same file.
riconalla
Member
66 Points
35 Posts
Error in sending email using MailMessage
Jul 07, 2011 10:34 AM|LINK
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 = ""; } } 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.
walia_jagwin...
Member
240 Points
173 Posts
Re: Error in sending email using MailMessage
Jul 07, 2011 12:46 PM|LINK
System.IO.IOException: The process cannot access the file 'c:\upload\07 - 2011.doc' because it is being used by another process. at
'c:\upload\07 - 2011.doc'
your file is already in use. Just restart the iis and try again. If still got the error then check the surcuity permissions
-- Jagz W
riconalla
Member
66 Points
35 Posts
Re: Error in sending email using MailMessage
Jul 07, 2011 02:12 PM|LINK
Thank you for your reply. I did restart iis and try again and the security permissions is right but still I got this error again. Any suggestions?
walia_jagwin...
Member
240 Points
173 Posts
Re: Error in sending email using MailMessage
Jul 07, 2011 02:24 PM|LINK
is your machine logined from administrator?
-- Jagz W
riconalla
Member
66 Points
35 Posts
Re: Error in sending email using MailMessage
Jul 07, 2011 02:36 PM|LINK
yes it's administrator.
walia_jagwin...
Member
240 Points
173 Posts
Re: Error in sending email using MailMessage
Jul 08, 2011 04:06 AM|LINK
Reasons of this error:-
1. This file is used in other process. (May file is in open mode) Close the file before debugging if it is open. Or some other application using the file.
2. You don't have the permissons to access the file. check file permission by right click on folder, go to security tab, select the user(......) options, now click the edit button , check the full control check box.
-- Jagz W
riconalla
Member
66 Points
35 Posts
Re: Error in sending email using MailMessage
Jul 11, 2011 09:01 AM|LINK
Thanks I follow what you told me but I got the same error when I try to send it again. Can you point out on where part of my code that needs to changed or add new code to make it work?
Anton Palyok
Contributor
2526 Points
404 Posts
Re: Error in sending email using MailMessage
Jul 11, 2011 09:01 PM|LINK
1) Try right click on "upload" folder and select "Properties".
2) Open "Security" tab -> Edit -> Add
3) Type "Everyone" and click OK
4) Select "Everyone" user and click "Allow" checkbox on "Full Control"
And Test uploading now
asurani15
Participant
902 Points
167 Posts
Re: Error in sending email using MailMessage
Jul 12, 2011 12:39 PM|LINK
The problem seems due to the loop in place.
Why to loop through all email addresses and send mail 1 by 1. Just loop through Email addresses, generate an Email address string where in each email address is separated by semicolon i.e. ';' and just pass the same to the 'To' attribute of the MailMessage object. Thats it.
Because it sends the mail in loop after the first iteration gets over, it doesn't release the file i.e. it still remains in use and then in the second iteration it starts to use the same file.
Try this and let me know what happened!
Mark this as Answer if it helps!
multiple
Mark as Answer if it helped!!! :)
riconalla
Member
66 Points
35 Posts
Re: Error in sending email using MailMessage
Jul 13, 2011 08:40 AM|LINK
Thank you. How can I achieve that?
multiple