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

i am sending the email using SMTP and adding the attachements to the email from file upload control and sending the attachements with mail,but for the first time if i am sending the file through email the mail is going correctly but after sending the mail, for the secoind time if i am adding the same file through file upload control i am getting theerror
"The process cannot access the file 'D:\Program Files\Express\Temp\SupportAttachments\support.txt' because it is being used by another process.
"


I am uploading the files from upload control to grid an dfrom grid picking the files and attaching to the mail..

here is my code:
C#
foreach (GridViewRow gvrow in grdfiledetails.Rows)
            {
                Label lbl = (Label)gvrow.FindControl("lblfilename");
                string SupportAttachmentFilePath = "D:\\Program Files\\Duck Creek Technologies\\Express\\Temp\\SupportAttachments";
                SupportAttachmentFilePath += "\\" + lbl.Text;
                string BasePathValue = lbl.Text;
                 firstAttachment = new Attachment(SupportAttachmentFilePath);
                firstAttachment = new Attachment(SupportAttachmentFilePath, MediaTypeNames.Application.Octet);
                mail.Attachments.Add(firstAttachment);
                //attach1 = BasePathValue;
                //firstAttachment.Dispose();
            }

            try
            {
                SmtpServer.Send(mail);}<pre lang="c#">


Please help what to do
Posted
Updated 7-Apr-14 2:19am
v2
Comments
[no name] 7-Apr-14 7:25am    
Well obviously, shutdown the process that is using that file before trying to use it.
Ajay_Babu 7-Apr-14 7:25am    
show me the code
spanner21 7-Apr-14 8:12am    
i am adding all the files to a grid and then sending the mail by picking the files from grid
foreach (GridViewRow gvrow in grdfiledetails.Rows)
{
Label lbl = (Label)gvrow.FindControl("lblfilename");
string SupportAttachmentFilePath = "D:\\Program Files\\Duck Creek Technologies\\Express\\Temp\\SupportAttachments";
SupportAttachmentFilePath += "\\" + lbl.Text;
string BasePathValue = lbl.Text;
firstAttachment = new Attachment(SupportAttachmentFilePath);
firstAttachment = new Attachment(SupportAttachmentFilePath, MediaTypeNames.Application.Octet);
mail.Attachments.Add(firstAttachment);
//attach1 = BasePathValue;
//firstAttachment.Dispose();
}

try
{
SmtpServer.Send(mail);
}

its my code
Murugesan22 7-Apr-14 10:42am    
set file path outside of forloop
spanner21 7-Apr-14 10:57am    
not getting...Could you please elaborate

Previous process has not been yet completed, thats y it is getting error,

it takes time to end the process

r else after sending email you end the process from code.

it may helps you...
 
Share this answer
 
The variable firstAttachment is declared outside the foreach loop - change that, declare it inside. Next, create the attachment only once; also make sure there are no duplicates (two attachments with the same filename)!
 
Share this answer
 
Comments
spanner21 7-Apr-14 8:44am    
1)I made sure there will be no duplicate attachements
2)firstAttachment is declared inside the foreach loop
3)made the attachment only once.

now the code is as shown:

foreach (GridViewRow gvrow in grdfiledetails.Rows)
{
Attachment firstAttachment;
Label lbl = (Label)gvrow.FindControl("lblfilename");
string SupportAttachmentFilePath = "D:\\Program Files\\Duck Creek Technologies\\Express\\Temp\\SupportAttachments";
SupportAttachmentFilePath += "\\" + lbl.Text;
string BasePathValue = lbl.Text;
//firstAttachment = new Attachment(SupportAttachmentFilePath);

firstAttachment = new Attachment(SupportAttachmentFilePath, MediaTypeNames.Application.Octet);
mail.Attachments.Add(firstAttachment);
//attach1 = BasePathValue;
//firstAttachment.Dispose();
}

try
{
SmtpServer.Send(mail);
}

but i saw in google that dispose will work...so i tried to write it in after
SmtpServer.Send(mail);
but not able to acess the object of Attachements which is firstAttachment here..

please help..

i am stuck here...The process cannot access the file error is hammering me

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