Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
  You are here: home »» Info Bank »» Articles » Creating an e-mail Web Service using Visual Studio.NET Saturday, 23 February 2008

Back to Articles Page      


Creating an e-mail Web Service using Visual Studio.NET
 Reproduced with kind permission of Dallas DotNet User Group External link
 Article written by: Casey Chesnut, iigo, Inc.(  E-mail )

  • Introduction
    A Web Service is a web application component that exposes its interface over the Internet. It is accessible through the standard web protocol HTTP GET/POST as well as the rapidly evolving standard SOAP (a combination of HTTP and XML). This lets other distributed applications access this resource without having to rewrite the same functionality. Thus, a web service is not application specific, and could be used by many different systems throughout the enterprise.

    This article demonstrates how to create a .NET Web Service using C#, for programmatically sending emails using CDO (Collaboration Data Objects) with Visual Studio.NET.

  • Steps
    1. Install CDO
      First off, you must install CDO (Collaboration Data Objects) to programmatically send email (You can still use this article to see how to create a Web Service without doing this). CDO can be installed from your Microsoft Office 2000 CDs. Just insert your installation CD, select to ‘Add or Remove Components’ and select to run Collaboration Data Objects from disk under Microsoft Outlook.

    2. Create Visual Studio Project
      Create a new Web Application Project in Visual Studio. I named this one EmailWebService.

    3. Create Web Service
      Use the solution explorer to Add a Web Service page to the project. I named this one emailService.asmx (Note: the file extension is not .aspx. The .asmx extension is used to designate Web Services, while .aspx designates Web Forms, which could be used to access the Web Service).

    4. View Web Service Source
      The Web Service will open in the Design View, but since this Web Service will not have a User Interface, just double click in the Design View to view the source code. This particular Web Service uses C#, although it could easily be written using VB.NET.

    5. Write Web Service Method
      At this point, there is just skeleton code (Although you can uncomment the standard HelloWorld() public method just to test that your environment is setup correctly). Exposing the service is just a matter of adding the public method preceded by the [WebMethod] compiler directive into the class. The code necessary to programmatically send email is listed below.
            //added for EmailWebService
            //dependent upon adding 'using System.Web.Util;' 
            //above for MailMessage, MailFormat, and SmtpMail class
            public string SendEmail(string toAddress, string fromAddress, 
                                    string subject, string messageBody)
                  string returnMessage = "SendEmail - blah";
                        MailMessage mailMsg = new MailMessage();
                        mailMsg.To = toAddress;
                        mailMsg.From = fromAddress;
                        mailMsg.Subject = subject;
                        mailMsg.BodyFormat = MailFormat.Text;
                        mailMsg.Body = messageBody;
                        returnMessage = "Message was successfully sent.";
                          returnMessage = "An error occured. Message was not sent.";
                      return returnMessage;

    6. Explore
      After building the project, the Web Service is browser accessible by directly loading emailService.asmx (e.g. http://localhost/EmailWebService/emailService.asmx). Through reflection, it creates an HTML interface that can be used for testing. If you had uncommented the HelloWorld() WebMethod, it would be exposed here as well.

    7. View SDL You can also view the SDL contract:

    8. Test
      To test the Web Service, just enter in valid email addresses, subject, and message bodies into the form and press the ‘Invoke’ button. By pressing the Invoke button, this email service was accessed through an HTTP GET shown below

      http://localhost/EmailWebService/emailService.asmx/[email protected][email protected]&subject=Test+Email&messageBody=Using+Web+Service

    9. View XML Response
      Your message was successfully sent. With the response returned in XML format, so that the application can handle it.

    10. Check the End Result
      And the result on the email client.

  • Summary
    This Web Service is now accessible across your entire enterprise. Everywhere one of your sites needs to send an email, it can leverage the use of this component. No matter what language your other sites are written in, wherever they need to send email, they can use this component. Which opens up many potential architectural uses. On one of my sites alone, I have used this service for such common applications as ‘Email your friends about this site’, ‘Verification or email addresses’, and ‘Email notices of changes/additions’.

    If you have any questions/comments, please contact author of this article, Casey Chesnut, at   E-mail .


Back to Articles Page      

  Contact Us |  | Site Guide | About PerfectXML | Advertise ©2004 All rights reserved. | Privacy