views:

34

answers:

1

Hi all,

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 ??

Thanks in advanced, greetings

+1  A: 

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 );
}
jlafay
I need sample of windows service that notify emails. If service has several threads, I save errors in List<T> , in database, in files ?? and then, when process finish, send email ?? thanks
alhambraeidos
SmtpClient DOES have a SendAsync() function to use if you want to prevent threads from blocking. You could write your method to pass in the List of errors so you can perform an async send method call on each error in the collection.I'm sorry that I can't reproduce an entire service here for you. Are you needing more help than emailing notifications? Please remember this site is for help, not to complete your work for you.
jlafay
Ask for only for any sample code in net or codeplex, sourceforge...not complete work for me, only reuse free good tested components , and do good practices and patterns
alhambraeidos
I'm sorry alhambraeidos, but I believe you're asking for a complete solution to your specific scenario which you're not going to find. You'll have to actually write the software yourself, or possibly hire someone that will.
jlafay