Error when trying to send email from Windows Service

G

Gerard

Hello

I have created a windows service to monitor a database, it starts some
checks when a timer elapses. The checks send emails depending on their
findings. My issue is that when I created a windows application it
worked fine, however I need to use a service as I don't want to rely
on a user being logged in. The errors I get are:

Index #:System.Web.HttpException: Could not access 'CDO.Message'
object. ---> System.Reflection.TargetInvocationException: Exception
has been thrown by the target of an invocation. --->
System.IO.FileNotFoundException: The specified module could not be
found.

--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags
invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers,
Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags
invokeAttr, Binder binder, Object target, Object[] args,
ParameterModifier[] modifiers, CultureInfo culture, String[]
namedParameters)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr,
Binder binder, Object target, Object[] args)
at System.Web.Mail.LateBoundAccessHelper.SetProp(Type type, Object
obj, String propName, Object propKey, Object propValue)
at System.Web.Mail.LateBoundAccessHelper.SetProp(Object obj, String
propName, Object propKey, Object propValue)
--- End of inner exception stack trace ---
at System.Web.Mail.LateBoundAccessHelper.SetProp(Object obj, String
propName, Object propKey, Object propValue)
at System.Web.Mail.CdoSysHelper.SetField(Object m, String name,
String value)
at System.Web.Mail.CdoSysHelper.Send(MailMessage message)
at System.Web.Mail.SmtpMail.Send(MailMessage message)
at SGMSTrafficMonitorService.Email.Send(String toAddress, String
subject, String body, String CCAddress, String BCCAddress) in
C:\_Development\Classes\Email.vb:line 79
Message: Could not access 'CDO.Message' object.
Inner Exception: Exception has been thrown by the target of an
invocation.
Source: System.Web

I have included System.Web.dll however it seems to be something else.
The code which sends the email is in its own class:

Public Function Send(ByVal toAddress As String, _
ByVal subject As String, _
ByVal body As String, _
Optional ByVal CCAddress As String = "", _
Optional ByVal BCCAddress As String = "") As
Boolean

Debug.Write("Send started")

' initialise return value...
Send = False

' variable which will send the mail...
Dim obj As System.Web.Mail.SmtpMail

' variable to create the message to send...
Dim msgMail As MailMessage = New MailMessage

' set the properties
' assign the SMTP server...
obj.SmtpServer = _appSettings.SMTPServerAddress

' address of the recipient(s)...
If Len(toAddress) > 0 Then msgMail.To = toAddress
If Len(CCAddress) > 0 Then msgMail.Cc = CCAddress
If Len(BCCAddress) > 0 Then msgMail.Bcc = BCCAddress

' the from address...
msgMail.From = _appSettings.EmailFrom

' specify the body format...
msgMail.BodyFormat = MailFormat.Html

' add a reply to header...
msgMail.Headers.Add("Reply-To", _appSettings.EmailReplyTo)

' mail subject...
msgMail.Subject = subject

' mail body...
msgMail.Body = body

Try
' call the send method to send the mail...
obj.Send(msgMail)

return True

Catch ex As Exception

Dim errMessage As String
errMessage = "Unable to send email, error details: " _
& "Index #:" & ex.ToString() &
ControlChars.NewLine _
& "Message: " & ex.Message &
ControlChars.NewLine _
& "Inner Exception: " &
ex.InnerException.Message _
& ControlChars.NewLine _
& "Source: " & ex.Source & ControlChars.NewLine

' log error...
_eventlog.WriteToEventLog(errMessage,
EventLogEntryType.Error)

' return failure...
return False

End Try

End Function

Sorry for the long post... Any help appreciated.

Cheers
Gerard
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Gerard) scripsit:
I have created a windows service to monitor a database, it starts some
checks when a timer elapses. The checks send emails depending on their
findings. My issue is that when I created a windows application it
worked fine, however I need to use a service as I don't want to rely
on a user being logged in. The errors I get are:

Index #:System.Web.HttpException: Could not access 'CDO.Message'
object. ---> System.Reflection.TargetInvocationException: Exception
has been thrown by the target of an invocation. --->
System.IO.FileNotFoundException: The specified module could not be
found.

Did you already have a look at <URL:http://www.systemwebmail.net/>?
 
G

Gerard

* (e-mail address removed) (Gerard) scripsit:

Did you already have a look at <URL:http://www.systemwebmail.net/>?

Yes I have looked and looked again.

I can take the exact same code, create a windows application and it
works. However anything I try within a windows service produces the
error. I am sure it is possible to send email from within windows
services, I just can't for the life of me work out how nor can I find
anything on google to help...

The heart of the error seems to be: "System.IO.FileNotFoundException:
The specified module could not be found." I would think this is
system.web.dll however it exists and as said, sending email using it
within a windows app works. I don't know much about services but would
imagine that all the links etc work the same whether you use a service
or an app in the .net framework.

Again, any help appreciated...
 
G

Gerard

Solution found

I didn't know enough about services, i.e. that you have to
specifically create a thread to do the work. You can't use the onstart
method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top