|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis is an ASP.NET web service for sending e-mail messages. When programming with .NET, sending e-mail doesn't seem to be a complicated task at all, you need only to know two simple framework classes, Suppose the SMTP service is enabled on your machine, here is a C# example of sending an e-mail message with attachment. MailMessage oMsg = new MailMessage();
oMsg.From = "me@MyServer.Net";
oMsg.To = "you@YourServer.Net";
oMsg.Cc = "him@HisServer.Net";
oMsg.Subject = "Test";
oMsg.Body = "This is only a test";
oMsg.Attachments.Add(new MailAttachment("c:\temp\Test.txt"));
SmtpMail.Send(oMsg);
If you copy/paste/modify the above code, all your programs can have e-mail capability and there is no need for any web service as far as e-mail is concerned. However, there are some advantages in providing e-mail capability in a web service, such as:
My simple e-mail web service has only one method, The web service for e-mailTo send an e-mail, you need to specify the destination address, the sender address, the list of addresses to copy the e-mail message, the subject line, the message body, and the list of attachment files, etc. Some of the information is not required. Things can get more complicated if you want to send multiple e-mails at once. Instead of having multiple web methods that take various numbers of parameters, I decided to implement a single method <EMailMessageList>
<EMailMessage>
<From></From>
<To></To>
<Cc></Cc>
<Bcc></Bcc>
<ReplyTo></ReplyTo>
<Subject></Subject>
<Body></Body>
<Format></Format>
<AttachmentList>
<Attachment></Attachment>
</AttachmentList>
</EMailMessage>
</EMailMessageList>
The The As you can see, it is possible to pack multiple e-mail messages in the input string parameter and each message can have multiple attachment files. Here is the C# code that uses the web service to send e-mail (for simplicity, the above XML string is saved in the file EMailTemplate.txt and used as a template for XML document in the code). // prepare the xml document
XmlDocument oDoc = new XmlDocument();
oDoc.Load("EMailTemplate.txt");
oDoc.SelectSingleNode("//From").InnerText = "me@MyServer.net";
oDoc.SelectSingleNode("//To").InnerText = "you@MyServer.net";
oDoc.SelectSingleNode("//Subject").InnerText = "Test";
oDoc.SelectSingleNode("//Body").InnerText = "This is a test";
// send the e-mail message
EMailService oWebSvcProxy = new EMailService();
oWebSvcProxy.Url = "http://MyServer.Net/TheEMailService/EMailService.asmx";
oWebSvcProxy.SendMail(oDoc.OuterXML);
Please note that Comparing to sending e-mails from individual programs, we may have to write a few more lines of code to use the e-mail web service. However, there are some additional advantages besides the ones listed above. For example, the web service writes all errors and debug information into a trace file. In case of failure, you can see exactly what is happening (what input the user is sending to the web service and what is causing the problem, etc.). The tracing capability in this web service is similar to my other components posted on Code Project, basically there will be one trace file for each day and the amount of information written to the trace file can be controlled by setting trace level from the web.config file. Also, old trace files will be deleted automatically to save disk space. To call the web service from C/C++/VB programs, you can use the Microsoft Toolkit 3.0. I also wrote a COM DLL XYSoapClient that uses the SOAP client object from the SOAP toolkit to simplify the code. Security and installationNow we have this web service ready, there is always a danger that the service will be exploited and misused. Any program on the same network can call this web service unless you restrict the access to it explicitly. Access to the web service can be restricted by configuring it from the Windows Internet Service Manager. It is possible to allow only access from the local machine and deny all other requests. You can grant or deny access for a group of IP addresses. You may also use Windows authentication or require a password. I will not go into details here. The included VB script file InstallEMailService.vbs can ease the pain of installation and configuration. First, you need to unzip the downloaded file into a folder on your machine. Then you run the VB script file, which will popup message boxes to ask you the following questions.
The installation script will automatically create virtual directory for the web service on the website you specified, copy files, and set access permissions. Thank you for reading my articles and using my tools. Recent Updates02/09/2004: Updated the install script file. The previous version does not set permissions properly.
|
||||||||||||||||||||||