Introduction
This article will show you how simple it is to create an Email Notification Web Service. But unfortunately I am still not able to include the attachment inside the email. It is quite easy to add an attachment if you are using point to point connection on your service client and service consumer. But if you have to include the Service Registry in between, then it will not be easy to achieve.
Background
I have started my web service development and WCF development recently. I found that the email notification service can be one of the functions or modules that can be reused on other applications. After failing to get some good examples, I decided to do it on my own. By the way the detail function and module of email sending in this article is also from Code Project. What I have done is just expose this email sending module from component layer to service layer. For those readers who know about SOA (Service Oriented Architecture), they will understand better what I am saying.
Create A New Web Service
With VS 2008, you can easily create a Web Service via the template inside the VS2008. By default, you should get an "ASMX" file and the C# file which is the background code for the "ASMX" file.
Expose your Component as Service
If you already have the email notification module on your own, just copy and paste into your web service project. If not, you can just get it from the zip file that I have uploaded together with this article.
This kind of service development enables you to reduce your effort to change your existing component to become a web service where later it can be reused in other applications or systems. Even though it is not a good example of SOA, but it can also be considered as SOA landscape in the IT landscape.
After copying the module, you can start creating an interface to link your service parameter with the module parameter.
Notification Service Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace EmailNotification
{
[WebService(Namespace = "http://MailServiceSample/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class SentEmail : System.Web.Services.WebService
{
[WebMethod]
public string Sending_Email(string strEmailAddrFrom,
string[] strEmailAddrTo, int intTotalEmailTo, string strAttachement)
{
EmailAlert NewMail = new EmailAlert();
return NewMail.EmailSent(strEmailAddrFrom,
strEmailAddrTo, intTotalEmailTo, strAttachement);
}
}
}
Example Notification Module Code
public string EmailSent(string strEmailAddrFrom,
string [] strEmailAddrTo, int intTotalEmailTo, string strAttachement)
{
string strSent= " ";
try
{
myMailMessage = new MailMessage();
for (int NumberOfEmails = 0;
NumberOfEmails < intTotalEmailTo; NumberOfEmails++)
{
myMailMessage.To.Add(new MailAddress
(strEmailAddrTo[NumberOfEmails]));
}
myMailMessage.From = new MailAddress(strEmailAddrFrom, "Admin");
( "addressOne@example.com" ));
( "addressTwo@example.com" ));
( "addressThree@example.com" ));
myMailMessage.Subject = "Error On Optimizer Program";
myMailMessage.Body = "Error On Optimizer Program.
Please check the detail from the attachment";
myMailMessage.IsBodyHtml = true;
myMailMessage.Priority = MailPriority.High;
if (strAttachement != "" || strAttachement != null)
{
Attachment att = new Attachment(strAttachement);
myMailMessage.Attachments.Add(att);
}
SmtpClient myMailClient = new SmtpClient();
myMailClient.Host = "Your Mail Host";
myMailClient.Port = 25;
myMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
myMailClient.SendCompleted += new SendCompletedEventHandler
(SendCompletedCallback);
object userState = myMailMessage;
try
{
Console.WriteLine("Mail Sending In progress");
myMailClient.Send(myMailMessage);
}
catch (System.Net.Mail.SmtpException ex)
{
Console.WriteLine(ex.Message, "Send Mail Error");
strSent = strSent + ex.Message;
}
myMailMessage.Dispose();
strSent = "Mail Sent !!";
}
catch (System.Net.Mail.SmtpException exSmtp)
{
Console.WriteLine("Exception occurred:" +
exSmtp.Message, "SMTP Exception Error");
strSent = strSent + "Exception occurred:" + exSmtp.Message;
}
catch (System.Exception exGen)
{
Console.WriteLine("Exception occurred:" +
exGen.Message, "General Exception Error");
strSent = strSent + "Exception occurred:" + exGen.Message;
}
return strSent;
}
History
- 6th November, 2008: Initial post