Advertisement
calfred2808

Email Sending Attachment Then Delete

Feb 14th, 2020 (edited)
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.92 KB | None | 0 0
  1. Public Sub email()
  2.     'import this  Imports System.IO
  3.     Dim Pathstore As String = String.Empty
  4.     Dim Pathcode As String = String.Empty
  5.  
  6.     With New StreamReader("C:\VB Test\location.txt")
  7.         Pathstore = .ReadLine()
  8.         .Dispose()
  9.     End With
  10.  
  11.     ' Are you sure this is the correct file ?
  12.     With New StreamReader("C:\VB Test\rmmsiul.dll")
  13.         Pathcode = .ReadLine()
  14.         .Dispose()
  15.     End With
  16.  
  17.     ' Capture the list of Attachment Files here, then use it twice below
  18.     Dim Attachments() As String = Directory.GetFiles("C:\VB Test\Receipts")
  19.  
  20.     Dim e_mail As New Net.Mail.MailMessage()
  21.     With e_mail
  22.         .From = New Net.Mail.MailAddress("Do-Not-Reply@suncommobile.com")
  23.         .Subject = String.Format("{0} Manual reciepts", Pathstore)
  24.         .Body = String.Format("Here are the manual reciepts I created today.{0}{0}{0}Thank you,{0}{1}", Environment.NewLine, Pathstore)
  25.  
  26.         ' Since I don't know what Filter() returns, this is best guess to reproduce the same outcome
  27.         For Each line As String In Filter(File.ReadAllLines("C:\VB Test\stores.txt"), Pathstore)
  28.             Dim fields() As String = line.Split(",")
  29.             .CC.Clear()
  30.             .CC.Add(fields(4))
  31.             .CC.Add(fields(2))
  32.             .CC.Add(fields(6))
  33.         Next
  34.  
  35.         For Each filepath In Attachments
  36.             .Attachments.Add(New Net.Mail.Attachment(filepath))
  37.         Next
  38.     End With
  39.  
  40.     With New Net.Mail.SmtpClient
  41.         .Host = "smtp.office365.com"
  42.         .Credentials = New Net.NetworkCredential("Do-Not-Reply@suncommobile.com", Pathcode)
  43.         .Port = 587
  44.         .EnableSsl = True
  45.         .Send(e_mail)
  46.     End With
  47.  
  48.     ' Dispose the MailMessage to release the holds on the Attachment Files
  49.     e_mail.Dispose()
  50.  
  51.     ' Delete the Attachment Files
  52.     For Each filepath In Attachments
  53.         File.Delete(filepath)
  54.     Next
  55.  
  56.     MsgBox("E-mail Sent.")
  57. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement