![]() |
Web Development »
ASP.NET »
General
Intermediate
A .NET web service for e-mailBy Xiangyang Liu 刘向阳Sending e-mail using a web service. |
C#, VB.NET 1.1, Win2K, WinXP, Win2003, ASP.NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This 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, MailMessage and SmtpMail, in the System.Web.Mail namespace. If you want to attach a file to your e-mail message, then you also need to know the MailAttachment class.
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, SendMail, which can be used to send multiple e-mails with a single call. Please note that you cannot use this web service to retrieve e-mail messages.
To 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 SendMail that takes only one string parameter and returns a boolean value to indicate success or failure. The string parameter represents an XML document, here is the format:
<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 From element is the sender's e-mail address. The To element is the destination address, it could be multiple e-mail addresses separated by semi-colons. The same goes with the Cc and the Bcc elements. The ReplyTo element is the e-mail address to be used when replying to this message, it can be different from the sender's e-mail address. The Subject and Body elements are obvious.
The Format element specifies the format of the message body, which can be either HTML or TEXT with TEXT as the default. The Attachment element holds the full path of a file on the local system. Please note that you cannot attach a file that is not on the server where the web service resides.
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 EMailService in the above code is the proxy class generated when you add a web reference for the e-mail service to your project.
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.
Now 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.
02/09/2004: Updated the install script file. The previous version does not set permissions properly.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 Feb 2005 Editor: Nishant Sivakumar |
Copyright 2004 by Xiangyang Liu 刘向阳 Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |