Introduction
This article will show you how simple to create a Email Notification Web Service. But unfortunately i am still not able to include the attachement inside the email. It is quite easy to add and attachement 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 be not easy to archieve.
Background
I have started my web service development and WCF development recently. I found that the email notification service can be one of the function or module that can be resue on other application. After failing to get some good example, i decide to do it on my owned. 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 reader who known about SOA (Service Oriented Architecture) will understand more about what i am saying.
Create A new web service
With VS2008 you can easily create and Web Service via the templete inside the VS2008. In default you shoud 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 owned, just copy and paste into your web service project. If not you can just get from the zip file that i have uploaded together with this article.
This kind of service development enable you to reduce your effort to change your existing component to become a web services where later it can be reuse in other applicaition or system. Even though it is not a good example of SOA but it can be also consider as SOA landscape in the IT landscape.
After copy the module you can start to create 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
{
///
/// Summary description for EmailNotification
///
[WebService(Namespace = "http://MailServiceSample/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
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
{
// Initializes a new instance of the System.Net.Mail.MailMessage class.
myMailMessage = new MailMessage();
// Obtains the e-mail address of the person the e-mail is being sent to.
for (int NumberOfEmails = 0; NumberOfEmails < intTotalEmailTo; NumberOfEmails++)
{
myMailMessage.To.Add(new MailAddress(strEmailAddrTo[NumberOfEmails]));
}
// Obtains the e-mail address of the person sending the message.
myMailMessage.From = new MailAddress(strEmailAddrFrom, "Admin");
// You can add additional addresses by simply calling .Add again.
// Support not added in the current example UI.
//
// myMailMessage.To.Add( new System.Net.Mail.MailAddress( "addressOne@example.com" ));
// myMailMessage.To.Add( new System.Net.Mail.MailAddress( "addressTwo@example.com" ));
// myMailMessage.To.Add( new System.Net.Mail.MailAddress( "addressThree@example.com" ));
// You can also specify a friendly name to be displayed within the e-mail
// application on the client-side for the To Address.
// Support not added in the current example UI.
// See the example below:
//
// myMailMessage.To.Add(new System.Net.Mail.MailAddress( this.txtToAddress.Text, "My Name Here" ));
// myMailMessage.From(new System.Net.Mail.MailAddress( this.txtToAddress.Text, "Another Name Here" ));
// System.Net.Mail also supports Carbon Copy(CC) and Blind Carbon Copy (BCC)
// Support not added in the current example UI.
// See the example below:
//
// myMailMessage.CC.Add ( new System.Net.Mail.MailAddress( "carbonCopy@example.com" ));
// myMailMessage.Bcc.Add( new System.Net.Mail.MailAddress( "blindCarbonCopy@example.com" ));
// Obtains the subject of the e-mail message
myMailMessage.Subject = "Error On Optimizer Program";
// Obtains the body of the e-mail message.
myMailMessage.Body = "Error On Optimizer Program. Please check the detail from the attachment";
// Listed below are the two message formats that can be used:
// 1. Text
// 2. HTML
//
// The default format is Text.
myMailMessage.IsBodyHtml = true;
// Listed below are the three priority levels that can be used:
// 1. High
// 2. Normal
// 3. Low
//
// The default priority level is Normal.
//
// This section of code determines which priority level
// was checked by the user.
myMailMessage.Priority = MailPriority.High;
//myMailMessage.Priority = MailPriority.Normal;
//myMailMessage.Priority = MailPriority.Low;
// Not Yet Implement
// This section of code determines if the e-mail message is going to
// have an attachment.
if (strAttachement != "" || strAttachement != null)
{
Attachment att = new Attachment(strAttachement);
myMailMessage.Attachments.Add(att);
}
// Custom headers can also be added to the MailMessage.
// These custom headers can be used to tag an e-mail message
// with information that can be useful in tracking an e-mail
// message.
//
// Support not added in the current example UI.
// See the example below:
// myMailMessage.Headers.Add( "Titan-Company", "Titan Company Name" );
// Initializes a new instance of the System.Net.Mail.SmtpClient class.
SmtpClient myMailClient = new SmtpClient();
// Obtains the email server name or IP address to use when sending the e-mail.
myMailClient.Host = "Your Mail Host";
// Defines the port number to use when connecting to the mail server.
// The default port number for SMTP is 25/TCP.
myMailClient.Port = 25;
// Specifies the delivery method to use when sending the e-mail
// message. Listed below are the three delivery methods
// that can be used by namespace System.Net.Mail
//
// 1. Network = sent through the network to an SMTP server.
// 2. PickupDirectoryFromIis = copied to the pickup directory used by a local IIS server.
// 3. SpecifiedPickupDirectory = is copied to the directory specified by the
// SmtpClient.PickupDirectoryLocation property.
myMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
// Initializes a new instance of the System.Net.NetworkCredential class.
//NetworkCredential myMailCredential = new NetworkCredential();
// Obtains the user account needed to authenticate to the mail server.
//myMailCredential.UserName = this.txtUserAccount.Text;
// Obtains the user password needed to authenticate to the mail server.
//myMailCredential.Password = this.txtUserPassword.Text;
// In this example we are providing credentials to use to authenticate to
// the e-mail server. Your can also use the default credentials of the
// currently logged on user. For client applications, this is the desired
// behavior in most scenarios. In those cases the bool value would be set to true.
//myMailClient.UseDefaultCredentials = true;
// Obtains the credentials needed to authenticate the sender.
//myMailClient.Credentials = myMailCredential;
// Set the method that is called back when the send operation ends.
myMailClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// Sends the message to the defined e-mail for processing
// and delivery with feedback.
//
// In the current example randomToken generation was not added.
//
//string randomToken = "randonTokenTestValue";
//myMailClient.SendAsync( myMailMessage, randomToken );
object userState = myMailMessage;
try
{
//you can also call myMailClient.SendAsync(myMailMessage, userState);
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 !!";
}
// Catches an exception that is thrown when the SmtpClient is not able to complete a
// Send or SendAsync operation to a particular recipient.
catch (System.Net.Mail.SmtpException exSmtp)
{
Console.WriteLine("Exception occurred:" + exSmtp.Message, "SMTP Exception Error");
strSent = strSent + "Exception occurred:" + exSmtp.Message;
}
// Catches general exception not thrown using the System.Net.Mail.SmtpException above.
// This general exception also will catch invalid formatted e-mail addresses, because
// a regular expression has not been added to this example to catch this problem.
catch (System.Exception exGen)
{
Console.WriteLine("Exception occurred:" + exGen.Message, "General Exception Error");
strSent = strSent + "Exception occurred:" + exGen.Message;
}
return strSent;
}
->Read More...
0 Comments:
Đăng nhận xét