玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

Webservice Send InBox

Posted on 2007-01-03 22:53 玄铁剑 阅读(471) 评论(0)  编辑 收藏 引用 所属分类: Service

Introduction

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:

  • A web service can be accessed by all servers on the network. You need only to enable SMTP service on a single machine. In other words, you can have better control over e-mail.
  • All of your non .NET programs can use the web service to send e-mail as well. So you don't have to write e-mail related code for your C/C++/VB/Java programs (but you do need to know how to call a web service from these programs).
  • You are not limited to the Windows platform, programs running on other platforms can use the e-mail service.
  • Programs can access the e-mail service across firewalls without additional firewall configuration (assuming HTTP traffic is allowed). Of course, you need to have authentication in this case so that the service won't be abused.

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.

The web service for e-mail

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.

Security and installation

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.

  1. What directory you want to install the web service?
  2. What is the website you want to install the web service? This is because some servers may have multiple websites and each of them is bound to a different IP address (or port number). You can use the "app friendly name" to distinguish websites. Just click OK if this does not apply to you.
  3. What is the port number of the website? Click OK if it is 80 (the default).
  4. Do you want to allow access from all IP addresses? If you choose "yes", then anyone can call the web service. Otherwise, only IP addresses from the same subgroup can access the web service. For example, if your website is bound to IP address 205.188.200.159, then only IP addresses of the form 205.188.200.* can call the web service you installed.

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 Updates

02/09/2004: Updated the install script file. The previous version does not set permissions properly.

Xiangyang Liu

只有注册用户登录后才能发表评论。