Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to delete a file which is attached to the mail after mail sent.
But it throws error of "The process cannot access file "c:/InVisitors.CSV" because it is being used by another process

i used the following code .......

path = "c:/InVisitors.CSV"
                    returnValue = VisDtlsOfEachVisitor
                    File.WriteAllText(path, returnValue)
                    Dim attach As Attachment = New Attachment("c:/InVisitors.CSV")
                    Mail.Attachments.Add(attach)
                    Message = "<HTML><BODY>" _
            & "Dear Sir,<br>" _
            & "<br>" _
            & "<br>" _
            & " Please find the attachment for the List of InVisitors.<br>" _
            & " And the List contains the Visitors Name and ID. " _
            & "<br>" _
            & "<br>" _
            & "Thanks & Regards,<br>" _
            & "iSpirit Technologies<br>" _
            & "<br>" _
            & "</BODY></HTML>"
                    Mail.Body = Message
                    Mail.IsBodyHtml = True
                    Mail.Priority = MailPriority.Normal
                    SmtpClient.EnableSsl = True
                    SmtpClient.Credentials = New Net.NetworkCredential(MailAdress, Password)
                    SmtpClient.Send(Mail)
                    Dim FileToDelete As String
                    FileToDelete = "c:/InVisitors.CSV"
                    If System.IO.File.Exists(FileToDelete) = True Then
                        System.IO.File.Delete(FileToDelete)
                    End If
                    MessageBox.Show(" Your message has been sent successfully ", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information</br></br></br></br></br></br></br></br></br>


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 16-Apr-11 12:18pm
v3

Hi Handle the SmtpClient's SendCompleted event to delete the file.

I was just thinking if the mail failed to receive what's your plan. Lose the attachment?
 
Share this answer
 
v2
Comments
Abhinav S 16-Apr-11 12:58pm    
Good answer. May help the OP if implemented correctly.
Albin Abel 16-Apr-11 14:34pm    
Thanks Abhinav. That is the only event available in the smtpClient class :)
Dalek Dave 16-Apr-11 18:19pm    
Good Call
Adding to Albin Abel's answer:
If the Mail and Smtp Classes are used in a local code block (rather than on Class level) it may also help if you properly Dispose of them before you try to delete the file. You should do this with a Try, Finally or Using, End Using statement (in case the mail fails, you still want to Dispose of it). If you use them on Class level you should Dispose of them too, of course, just not at this particular point in your code.


parshu2378 wrote:
SmtpClient.Credentials = New Net.NetworkCredential(MailAdress, Password)
SmtpClient.Send(Mail)


Just want to say the, you should take the delivery notification, which will ensure the process flow.
 
Share this answer
 
v3
Comments
Sander Rossel 18-Apr-11 11:43am    
Is there a reason for modifying my post for this or did you simply misclick?

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