Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.56 KB | None | 0 0
  1. Dim olApp as Outlook.Application
  2. ' This is only for if you already have Outlook open
  3. Set olAPP = CreateObject("Outlook.Application")
  4. ' If you aren't logged on
  5. Dim olNs as Outlook.NameSpace
  6. Set olNs = olApp.GetNamespace("MAPI")
  7. olNs.Logon
  8. 'Open SQL connection
  9. Dim myConnection As SqlConnection
  10. Dim myCommand As SqlCommand
  11. Dim dr As New SqlDataReader()
  12. myConnection = New SqlConnection("server=mysqldb.example.com;uid=user;pwd=pass;database=abg")
  13. 'Begin try block to catch errors
  14. Try
  15. myConnection.Open()
  16. 'Set query
  17. myCommand = New SqlCommand("SELECT buyer_email, buyer_name, project_name, deadline FROM Projects", myConnection)
  18. 'Run query
  19. dr = myCommand.ExecuteReader()
  20. 'Loop through each tuple in the query result - dr(#) refers to the attribute returned from the query
  21. 'e.g. 0 = buyer_email, 1 = buyer_name, 2=project_name
  22. While dr.Read()
  23.     'Create new message
  24.     Dim olMail As Outlook.MailItem
  25.     Set olMail = olApp.CreateItem(olMailItem)
  26.     'The inspector basically just appends your default signature, if the documentation I found is correct
  27.     Set olApp = olMail.GetInspector
  28.     'Set mail fields
  29.     olMail.To = dr(0).ToString()
  30.     olMail.Subject = "Update on " & dr(2).ToString()
  31.     olMail.Body = "Dear " & dr(1).ToString() & "," & vbCr & vbCR & "We would like to update you on your project with Allied Business Group. We have set a deadline for " & dr(3).ToString() & ". If this is an unacceptable timeframe, please contact me as soon as possible etc...." & vbCr & vbCr & "Regards," & vbCr
  32.     olMail.Send
  33. End While
  34. dr.Close()
  35. myConnection.Close()
  36. Catch e As Exception
  37. End Try
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement