Click here to Skip to main content
Click here to Skip to main content

Send Mails from Windows Application

, 15 Sep 2009
Rate this:
Please Sign up or sign in to vote.
This article enables you to send mails from Windows application which is normally a tedious task

Introduction

Usually sending mails from Windows application is a tedious task. Moreover, many times you may have to enable IIS on each machine. The article describes an alternate mechanism of sending mail from Windows application.

Background

The above described mechanism (of enabling IIS on each machine) would be feasible for applications having a small user pool. However, in applications where the number of users is high, enabling the IIS and setting up SMTP on each desktop would not be feasible. Also, sending mail from CDO conflicts with Exchange Server. This article describes an alternate approach for the same.

Using the Code

Developers may find thousands of sites on the Internet mentioning how to send mail through Windows applications. Majority of them would need the IIS to be enabled on each desktop. Details of the same can be found here. I tried to search for an alternative mechanism where we need to force each user to enable SMTP. In this article, we use a Web Service which is hosted on the Database Server. Developers can host the service on any other specific web server as well.

The diagram below shows the architecture involved:

To implement this functionality, we follow the steps given below: 

  1. To create a Web Service which would have the web method accepting the related parameters and would send mail to the assigned person: Add references of System.Web.Mail and System.Net. Later add the web method to send mail to the concerned person like the one shown below:
    string sTo =  "mysmtp@inf.com";
    string sFrom = conn;
    
    string sBody = "";
    
    try
    {
        MailMessage mail = new MailMessage();
        mail.From = sFrom.ToString();           	// put the from address here	
        mail.To = sTo.ToString();             	// put the to address here		
        mail.Subject = "Request To Work on Item";	// put the subject here	
        mail.Body = sBody.ToString();
        mail.BodyFormat = System.Web.Mail.MailFormat.Html;
        SmtpMail.SmtpServer = "10.66.236.54";
        SmtpMail.Send(mail);
        return "Sent";
    }
    catch (Exception e)
    {
        return "error";
    }

    In the application, we have added a lot of formatting in the body. If you wish, you can opt for the same as shown below:

    string sBody = "<table><tr><td colspan =&#39;2&#39;>Dear " + sSenderName + "," + 
            "</td></tr>" + "<tr><td colspan =&#39;2&#39;> </td></tr>" + 
            "<tr><td colspan =&#39;2&#39;>" + 
            "The details of the Item is shown in the table." + "</td></tr>" + "<tr><td 
            colspan =&#39;2&#39;> </td></tr>" + 
            "<tr><td><font color = green>Date Raised </td><td>: 
            </font>" + sDateRaised + "</td>" + "</tr>" + "<br>" + 
            "<tr><td><font color = 
            green>Index </td><td>: </font>" + sIndex + "</td>" + "</tr>" + "<br>" + 
            "<tr><td><font color = green>Description </td><td>: 
            </font>" + sDescription + 
            "</td></tr>" + "<br>" + "<br>" + "<tr><td><font color = green>Priority 
            </td><td>: </font>" + sPriority + "</td>" + "</tr>" 
            + "<br>" + "<tr><td><font 
            color = green>Status </td><td>: </font>" 
            + sStatus + "</td>" + "</tr>" + "<br>" 
            + "<tr><td><font color = green>Status Comments </td><td>: </font>" + 
            sStausComments + "</td>" + "</tr>" + "<br>" + 
            "<tr><td colspan =&#39;2&#39;>Thanks," + 
            "</td></tr>" + "<tr><td colspan =&#39;2&#39;>Sushant Joshi." 
            + "</td></tr>" + 
            "</table>";
  2. In the next step, we create the Windows form and create all the fields. 
  3. Later, we need to create the proxy for the Web Service.
    To create the proxy, you need to open Microsoft Visual Studio command prompt and run the following command:
    wsdl /language:C# /out:MyProxyClass.cs ttp://localhost/ASP.NET/MyWebService.asmx

    Details on creating Web Proxy can be found here

  4. In the last step, we add the proxy to the Windows Form project or any Business Layer project - in case of n tier application.
    After this, we can call individual web methods of the web service. Call to the mailing functionality is as shown below:
    Service1 MYCLS = new Service1();
    MYCLS.SendMail(sSenName, sDateRaised, sIndex, sDescription, 
    				sPriority, sStatus, sStausComments);

Finally, the entire communication looks as shown below:

Pop-up of Windows form (expecting inputs):

Pop-up of Windows form (expecting inputs) along with Web Service page:

Click to enlarge image

Points of Interest

Sending mails to users using Windows application can be done by enabling SMTP on each machine for a small set of users. However, in case of a large user pool, we use Web Service/Remoting call to send mail, which involves only enabling SMTP on one machine and could be a preferred option.

History

  • 15th September, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sushant Joshi
Software Developer (Senior)
India India
Working as a developer since last four years. Enjoys writting code snippets and exploring ASP.Net. You would find me either watching a movie or programming/devloping some reusable code snippets.

Comments and Discussions

 
QuestionCode Please ? PinmemberVasudeoPhadke21-Oct-09 22:02 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 15 Sep 2009
Article Copyright 2009 by Sushant Joshi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid