使用VB Studio发送电子邮件后删除文件时出错

时间:2016-07-21 20:14:25

标签: vb.net

下面是我用来发送附带文档的电子邮件的电子邮件代码,但是当我尝试删除文件时,它显示文件正在使用中。任何帮助将不胜感激。

Sub email()
    Dim Smtp_Server As New SmtpClient
    Dim e_mail As New MailMessage()
    Dim body As String
    Dim address As String
    Dim address2 As String
    Dim address3 As String
    Dim fileReader As System.IO.StreamReader

    fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\VB Test\location.txt")
    Dim Pathstore As String
    Pathstore = fileReader.ReadLine()

    'email address
    Dim lines() As String = System.IO.File.ReadAllLines("C:\VB Test\stores.txt")

    For Each line As String In Filter(lines, Pathstore)
        Dim fields() As String = line.Split(",")
        address = fields(4)
        address2 = fields(2)
        address3 = fields(6)
    Next

    Dim fileReader2 As System.IO.StreamReader
    fileReader2 = My.Computer.FileSystem.OpenTextFileReader("C:\VB Test\rmmsiul.dll")
    Dim Pathcode As String
    Pathcode = fileReader2.ReadLine()
    fileReader2.Close()

    body = "Here are the manual reciepts I created today." + vbNewLine + vbNewLine + vbNewLine & "Thank you," + vbNewLine + Pathstore


    Smtp_Server.UseDefaultCredentials = False
    Smtp_Server.Credentials = New Net.NetworkCredential("Do-Not-Reply@suncommobile.com", Pathcode)
    Smtp_Server.Port = 587
    Smtp_Server.EnableSsl = True
    Smtp_Server.Host = "smtp.office365.com"

    e_mail = New MailMessage()
    e_mail.From = New MailAddress("Do-Not-Reply@suncommobile.com")
    e_mail.CC.Add(address)
    e_mail.CC.Add(address2)
    e_mail.CC.Add(address3)
    e_mail.Subject = Pathstore + " Manual reciepts"
    e_mail.IsBodyHtml = False
    e_mail.Body = body
    Dim filepath As String
    For Each filepath In Directory.GetFiles("C:\VB Test\Receipts")
        Dim Attach As New Net.Mail.Attachment(filepath)
        e_mail.Attachments.Add(Attach)
        Kill(filepath)
    Next
    Smtp_Server.Send(e_mail)

    MsgBox("E-mail Sent.")
    Module1.filedelete()

End Sub

'将部分代码更改为以下内容,但在发送电子邮件时收到错误。

    For Each filepath As String In Directory.GetFiles("C:\VB Test\Receipts")
        Using reader As New StreamReader(filepath)
            Dim a As New Net.Mail.Attachment(reader.BaseStream, filepath)
            e_mail.Attachments.Add(a)
        End Using
    Next
    Smtp_Server.Send(e_mail)

1 个答案:

答案 0 :(得分:0)

Public Sub email()
    Dim Pathstore As String = String.Empty
    Dim Pathcode As String = String.Empty

    With New StreamReader("C:\VB Test\location.txt")
        Pathstore = .ReadLine()
        .Dispose()
    End With

    ' Are you sure this is the correct file ?
    With New StreamReader("C:\VB Test\rmmsiul.dll")
        Pathcode = .ReadLine()
        .Dispose()
    End With

    ' Capture the list of Attachment Files here, then use it twice below
    Dim Attachments() As String = Directory.GetFiles("C:\VB Test\Receipts")

    Dim e_mail As New Net.Mail.MailMessage()
    With e_mail
        .From = New Net.Mail.MailAddress("Do-Not-Reply@suncommobile.com")
        .Subject = String.Format("{0} Manual reciepts", Pathstore)
        .Body = String.Format("Here are the manual reciepts I created today.{0}{0}{0}Thank you,{0}{1}", Environment.NewLine, Pathstore)

        ' Since I don't know what Filter() returns, this is best guess to reproduce the same outcome
        For Each line As String In Filter(File.ReadAllLines("C:\VB Test\stores.txt"), Pathstore)
            Dim fields() As String = line.Split(",")
            .CC.Clear()
            .CC.Add(fields(4))
            .CC.Add(fields(2))
            .CC.Add(fields(6))
        Next

        For Each filepath In Attachments
            .Attachments.Add(New Net.Mail.Attachment(filepath))
        Next
    End With

    With New Net.Mail.SmtpClient
        .Host = "smtp.office365.com"
        .Credentials = New Net.NetworkCredential("Do-Not-Reply@suncommobile.com", Pathcode)
        .Port = 587
        .EnableSsl = True
        .Send(e_mail)
    End With

    ' Dispose the MailMessage to release the holds on the Attachment Files
    e_mail.Dispose()

    ' Delete the Attachment Files
    For Each filepath In Attachments
        File.Delete(filepath)
    Next

    MsgBox("E-mail Sent.")
End Sub