2016-07-21 3 views
0

添付ファイル付きの電子メールを送信するために使用している電子メールコードはありますが、ファイルを削除しようとすると、どんな助けもありがとう。VB Studioを使用して電子メールを送信した後にファイルを削除する際にエラーが発生しました

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("[email protected]", Pathcode) 
    Smtp_Server.Port = 587 
    Smtp_Server.EnableSsl = True 
    Smtp_Server.Host = "smtp.office365.com" 

    e_mail = New MailMessage() 
    e_mail.From = New MailAddress("[email protected]") 
    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

あなたは[のMemoryStream]にファイルをロードする場合(https://msdn.microsoft.com/en-us/library/system.io.memorystream(V = vs.110).aspxの)ファイルの代わりにMemoryStreamをアタッチすると、ファイルを削除できるはずです。メールを送信する際に問題が発生すると、すべてが失われるため、後で削除するためにファイルを別のディレクトリに移動する方が賢明かもしれません。 –

+0

削除しない理由は、あなたのMailMessage(e_mail)がまだファイルを指すハンドルを持っているからです。 @AndrewMortonは、何か問題が生じた場合に、他の場所でそれらのファイルをコピーしたいかもしれません。または、各ファイルパスを電子メールの送信後に後で削除できるように、リストまたはアレイに追加します。 –

+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("[email protected]") 
     .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("[email protected]", 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 
+0

ありがとう、それは美しく働いた。 –

関連する問題