在vb.net中发送邮件后删除文件 [英] Delete File after sent mail in vb.net

查看:71
本文介绍了在vb.net中发送邮件后删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除邮件发送后附加到邮件的文件。

但它抛出错误进程无法访问文件c:/InVisitors.CSV因为它正在由另一个进程使用



i使用以下代码.......



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>





已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

您可以将SmtpClient的 SendCompleted 事件处理到删除文件。



我只是在想邮件是否未能收到您的计划。丢失附件?
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?


添加到Albin Abel的答案:

如果Mail和Smtp类用于本地代码块(而不是Class)如果你在尝试删除文件之前正确处理它们,它也可能有帮助。您应该使用Try,Finally或Using,End Using语句执行此操作(如果邮件失败,您仍想要Dispose它)。如果你在Class级别使用它们,你也应该处理它们,当然,不要在代码中的这个特定点。





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写道:
parshu2378 wrote:

SmtpClient.Credentials = Net.NetworkCredential(MailAdress,密码) )

SmtpClient.Send(Mail)

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.


这篇关于在vb.net中发送邮件后删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆