Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / IIS

Send Mails from Windows Application

4.40/5 (95 votes)
15 Sep 2009CPOL2 min read 99K  
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:

Image 1

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:
    C#
    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:

    C#
    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:
    C#
    Service1 MYCLS = new Service1();
    MYCLS.SendMail(sSenName, sDateRaised, sIndex, sDescription, 
    				sPriority, sStatus, sStausComments);

Finally, the entire communication looks as shown below:

Image 2

Pop-up of Windows form (expecting inputs):

Image 3

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)