vb.net - error deleting files after I send an email using VB Studio -


below email code using send email attached document, when try delete files showing files in use. appreciated.

sub email()     dim smtp_server new smtpclient     dim e_mail new mailmessage()     dim body string     dim address string     dim address2 string     dim address3 string     dim filereader system.io.streamreader      filereader = my.computer.filesystem.opentextfilereader("c:\vb test\location.txt")     dim pathstore string     pathstore = filereader.readline()      'email address     dim lines() string = system.io.file.readalllines("c:\vb test\stores.txt")      each line string in filter(lines, pathstore)         dim fields() string = line.split(",")         address = fields(4)         address2 = fields(2)         address3 = fields(6)     next      dim filereader2 system.io.streamreader     filereader2 = my.computer.filesystem.opentextfilereader("c:\vb test\rmmsiul.dll")     dim pathcode string     pathcode = filereader2.readline()     filereader2.close()      body = "here manual reciepts 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 string     each filepath in directory.getfiles("c:\vb test\receipts")         dim attach 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 

'changed part of code following, getting error when sending email.

    each filepath string in directory.getfiles("c:\vb test\receipts")         using reader new streamreader(filepath)             dim new net.mail.attachment(reader.basestream, filepath)             e_mail.attachments.add(a)         end using     next     smtp_server.send(e_mail) 

public sub email()     dim pathstore string = string.empty     dim pathcode string = string.empty      new streamreader("c:\vb test\location.txt")         pathstore = .readline()         .dispose()     end      ' sure correct file ?     new streamreader("c:\vb test\rmmsiul.dll")         pathcode = .readline()         .dispose()     end      ' capture list of attachment files here, use twice below     dim attachments() string = directory.getfiles("c:\vb test\receipts")      dim e_mail new net.mail.mailmessage()     e_mail         .from = new net.mail.mailaddress("do-not-reply@suncommobile.com")         .subject = string.format("{0} manual reciepts", pathstore)         .body = string.format("here manual reciepts created today.{0}{0}{0}thank you,{0}{1}", environment.newline, pathstore)          ' since don't know filter() returns, best guess reproduce same outcome         each line string in filter(file.readalllines("c:\vb test\stores.txt"), pathstore)             dim fields() string = line.split(",")             .cc.clear()             .cc.add(fields(4))             .cc.add(fields(2))             .cc.add(fields(6))         next          each filepath in attachments             .attachments.add(new net.mail.attachment(filepath))         next     end      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      ' dispose mailmessage release holds on attachment files     e_mail.dispose()      ' delete attachment files     each filepath in attachments         file.delete(filepath)     next      msgbox("e-mail sent.") end sub 

Comments