Send method not working in windows service
Hi all
I have a windows service application to send emails.
The same code works well with windows application but not with windows service.
Code:
Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
EventLog.WriteEntry("Started!!!!")
Dim cmd As System.Data.OleDb.OleDbCommand
Dim dr As System.Data.OleDb.OleDbDataReader
Dim con As System.Data.OleDb.OleDbConnection
Dim oMsg As Outlook._MailItem
Dim objOL As Outlook.Application
objOL = New Outlook.Application()
Dim t As String
con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\CAD Audit\CAD_Audit.mdb;") ' User Id=admin; Password=")
Using sa As StreamReader = New StreamReader("C:\CAD Audit\job.log", System.Text.Encoding.Default)
t = sa.ReadLine()
While sa.Peek >= 0
t = sa.ReadLine()
'EventLog.WriteEntry("started1.")
If t.Contains("OK") Then
ElseIf t.Contains("FAILED REPLACE") Then
' MsgBox(t)
Dim groupe As String = ""
Dim fullGroupe As String = ""
fullGroupe = Trim(t.Substring(0, 8))
groupe = Trim(t.Substring(2, 3))
'MsgBox(groupe)
EventLog.WriteEntry("started2.")
cmd = New OleDb.OleDbCommand("select e.Email_ID,e.Lead_Name,e.Groupe from List_of_Email_ID e where (e.Groupe= " & groupe & ")", con)
con.Open()
dr = cmd.ExecuteReader()
While dr.Read()
EventLog.WriteEntry("started3.")
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
oMsg.Subject = "AUDIT CAO UNITES - ERREUR DE REMPLACEMENT"
oMsg.Body = "Bonjour " & dr.Item(1) & vbLf & vbCr & "il semble qu'il y ait un problème de remplacement avec l'unité " & fullGroupe & vbLf & vbCr & " En tant que responsable CAO du groupe, merci de faire la correction" & vbLf & vbCr & "Ne pas prendre ce message en compte s'il s'agit d'un kit " & vbLf & vbCr & "Salutations"
oMsg.To = dr.Item(0).ToString
EventLog.WriteEntry("started4.")
oMsg.Send()
EventLog.WriteEntry("started5.")
oMsg = Nothing
End While
con.Close()
objOL = Nothing
ElseIf t.Contains("DESTINATION MISSING") Then
'MsgBox(t)
Dim groupe As String = ""
Dim fullGroupe As String = ""
fullGroupe = Trim(t.Substring(0, 8))
groupe = Trim(t.Substring(2, 3))
'MsgBox(groupe)
cmd = New OleDb.OleDbCommand("select e.Email_ID,e.Lead_Name,e.Groupe from List_of_Email_ID e where (e.Groupe= " & groupe & ")", con)
con.Open()
dr = cmd.ExecuteReader()
While dr.Read()
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
oMsg.Subject = "AUDIT CAO UNITES - DES MISSING"
oMsg.Body = "Bonjour " & dr.Item(1) & vbLf & vbCr & "Le groupe " & fullGroupe & vbLf & vbCr & " n'est pas dans les trames " & vbLf & vbCr & "Salutations"
oMsg.To = dr.Item(0).ToString
oMsg.Send()
oMsg = Nothing
End While
con.Close()
objOL = Nothing
ElseIf t.Contains("SOURCE MISSING") Then
'MsgBox(t)
Dim groupe As String = ""
Dim fullGroupe As String = ""
fullGroupe = Trim(t.Substring(0, 8))
groupe = Trim(t.Substring(2, 3))
' MsgBox(groupe)
cmd = New OleDb.OleDbCommand("select e.Email_ID,e.Lead_Name,e.Groupe from List_of_Email_ID e where (e.Groupe= " & groupe & ")", con)
con.Open()
dr = cmd.ExecuteReader()
While dr.Read()
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
oMsg.Subject = "AUDIT CAO UNITES - ERREUR DE REMPLACEMENT"
oMsg.Body = "Bonjour " & dr.Item(1) & vbLf & vbCr & "il semble qu'il y ait un problème de remplacement avec l'unité " & fullGroupe & vbLf & vbCr & " En tant que responsable CAO du groupe, merci de faire la correction" & vbLf & vbCr & "Ne pas prendre ce message en compte s'il s'agit d'un kit" & vbLf & vbCr & "Salutations"
oMsg.To = dr.Item(0).ToString
oMsg.Send()
oMsg = Nothing
End While
con.Close()
objOL = Nothing
ElseIf t.Contains("FAILED SOURCE") Then
'MsgBox(t)
Dim groupe As String = ""
Dim fullGroupe As String = ""
fullGroupe = Trim(t.Substring(0, 8))
groupe = Trim(t.Substring(2, 3))
'MsgBox(groupe)
cmd = New OleDb.OleDbCommand("select e.Email_ID,e.Lead_Name,e.Groupe from List_of_Email_ID e where (e.Groupe= " & groupe & ")", con)
con.Open()
dr = cmd.ExecuteReader()
While dr.Read()
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
oMsg.Subject = "AUDIT CAO UNITES - ECHEC"
oMsg.Body = "Bonjour " & dr.Item(1) & vbLf & vbCr & "il semble qu'il y ait un échec dans l'unité " & fullGroupe & vbLf & vbCr & " En tant que responsable CAO du groupe, merci de faire la correction " & vbLf & vbCr & "Ne pas prendre ce message en compte s'il s'agit d'un kit" & vbLf & vbCr & "Salutations"
oMsg.To = dr.Item(0).ToString
oMsg.Send()
oMsg = Nothing
End While
con.Close()
objOL = Nothing
Else
End If
End While
End Using
End Sub
The is not working.
Any help will be appriciated.
Re: Send method not working in windows service
vb Code:
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
make sure outlook is installed and has a proper profile in the server hosting the webservice.
OR
use SMTPClient class. http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx