Click here to Skip to main content
Licence 
First Posted 4 Feb 2004
Views 170,869
Bookmarked 97 times

A .NET web service for e-mail

By | 17 Feb 2005 | Article
Sending e-mail using a web 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberJack_3218:30 9 May '12  
GeneralMy vote of 5 Pinmemberboyewa6:19 27 Apr '11  
GeneralEmail Web Service Pinmemberminibansal5:24 25 Jun '08  
GeneralSetting up security PinmemberGodwin Sam Josh18:25 3 Jul '06  
QuestionAttachment ? PinmemberProgzz7:04 23 Jan '06  
QuestionAnother Addition Possible? PinmemberRajen Suchede8:30 4 Jul '05  
AnswerRe: Another Addition Possible? PinmemberXiangyang Liu1:53 5 Jul '05  
QuestionWhy XML ? Pinmemberdapoussin21:34 28 Mar '05  
Why do you use XML for input ? It would be simpler to use parameters like To, From, Server, Body, etc. And you could send attachments in byte[] (with file name in another parameter).
AnswerRe: Why XML ? PinmemberXiangyang Liu0:01 29 Mar '05  
GeneralRe: Why XML ? Pinmemberdapoussin23:04 17 Apr '05  
GeneralRe: Why XML ? PinmemberLucky_hemya13:06 27 Jan '06  
GeneralUpdate 2005/02/16 PinmemberXiangyang Liu3:49 16 Feb '05  
GeneralRe: Update 2005/02/16 PinmemberYuen Chiu So22:43 17 Feb '05  
GeneralRe: Update 2005/02/16 PinmemberXiangyang Liu0:05 18 Feb '05  
Generalalomost trash Pinmemberyouyou19763:07 27 Mar '04  
GeneralRe: alomost trash PinmemberXiangyang Liu6:31 27 Mar '04  
GeneralTrace Info PinmemberMichael Cui10:37 13 Feb '04  
GeneralRe: Trace Info PinmemberXiangyang Liu14:22 13 Feb '04  
Generaladvantage of web service... PinmemberAshley van Gerven12:56 6 Feb '04  
GeneralRe: advantage of web service... PinmemberXiangyang Liu3:26 7 Feb '04  
GeneralRe: advantage of web service... Pinmemberkbuchan3:58 10 Feb '04  
GeneralRe: advantage of web service... Pinmemberaxelriet21:01 17 Feb '05  
GeneralRe: advantage of web service... PinmemberYuen Chiu So23:10 17 Feb '05  
GeneralRe: advantage of web service... PinmemberAshaman1:17 18 Feb '05  
GeneralQueueing for retry PinmemberPeter Wone13:12 20 Feb '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 17 Feb 2005
Article Copyright 2004 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid