Kiquenet
Kiquenet

Reputation: 14996

Good Sample .Net Windows service to report an error

I am writing a windows service that will be doing a lot of network communication (copy many many files in shared folders and modify database).

I need a way to notify a user (if logged on) of any exceptions/errors. My issue is do group to errors for send it (by email) to administrator address.

I am aware of event logging neither notification bubble from system tray, but the administrator not views that log, he prefers email.

The ideal component is ASP.NET Health Monitoring, but only works for IIS, and it is required a component similar for Windows Services.

Any sample application Windows Service in C# or another language in .net (with source code) about this issue ??

Upvotes: 1

Views: 1263

Answers (2)

HokaHelal
HokaHelal

Reputation: 1568

check this : http://www.codeproject.com/Articles/16335/Simple-Windows-Service-which-sends-auto-Email-aler

my advice if it's first time to work with windows service (because Windows Service is very hard to debug)

  • first make it Command Line application with c#
  • when you be certain with all functionality of your project move it to Windows Service

Upvotes: 0

Jeff LaFay
Jeff LaFay

Reputation: 13350

If you're just needing a way to send an email notification, .Net has SMTP and mail types to take care of this. Email notifications are common in software anymore and that's why the commonly used functionality has been incorporated into the base class lib.

You could use SmtpClient, MailAddress, and MailMessage objects to accomplish what you need to simply send an email notification. Of course you need to have access to an SMTP server to transmit the mail, so find out what its host address is to properly configure your application. Here are a few examples:

SmtpClient  mailClient = new SmtpClient("smtp.fu.bar");
MailAddress senderAddr = new MailAddress("[email protected]");
MailAddress  recipAddr = new MailAddress("[email protected]");
MailMessage   emailMsg = new MailMessage( senderAddr, recipAddr );

emailMsg.Subject = "Test email.";
emailMsg.Body = "Here is my email string which serves as the body.\n\nSincerely,\nMe";
mailClient.Send( emailMsg );

That example is just straight code, but it would be better to put it into a reusable method like this:

public void SendNotification( string smtpHost, string recipientAddress, string senderAddress, string message, string subject )
{
    SmtpClient  mailClient = new SmtpClient(smtpHost);
    MailMessage   emailMsg = new MailMessage( new MailAddress(senderAddress), new MailAddress(recipientAddress) );

    emailMsg.Subject = subject;
    emailMsg.Body = message;

    mailClient.Send( emailMsg );
}

Upvotes: 2

Related Questions