Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dim olApp as Outlook.Application
- ' This is only for if you already have Outlook open
- Set olAPP = CreateObject("Outlook.Application")
- ' If you aren't logged on
- Dim olNs as Outlook.NameSpace
- Set olNs = olApp.GetNamespace("MAPI")
- olNs.Logon
- 'Open SQL connection
- Dim myConnection As SqlConnection
- Dim myCommand As SqlCommand
- Dim dr As New SqlDataReader()
- myConnection = New SqlConnection("server=mysqldb.example.com;uid=user;pwd=pass;database=abg")
- 'Begin try block to catch errors
- Try
- myConnection.Open()
- 'Set query
- myCommand = New SqlCommand("SELECT buyer_email, buyer_name, project_name, deadline FROM Projects", myConnection)
- 'Run query
- dr = myCommand.ExecuteReader()
- 'Loop through each tuple in the query result - dr(#) refers to the attribute returned from the query
- 'e.g. 0 = buyer_email, 1 = buyer_name, 2=project_name
- While dr.Read()
- 'Create new message
- Dim olMail As Outlook.MailItem
- Set olMail = olApp.CreateItem(olMailItem)
- 'The inspector basically just appends your default signature, if the documentation I found is correct
- Set olApp = olMail.GetInspector
- 'Set mail fields
- olMail.To = dr(0).ToString()
- olMail.Subject = "Update on " & dr(2).ToString()
- 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
- olMail.Send
- End While
- dr.Close()
- myConnection.Close()
- Catch e As Exception
- End Try
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement