Click here to Skip to main content
15,867,453 members
Articles / Web Development / IIS

Send Mails from Windows Application

Rate me:
Please Sign up or sign in to vote.
4.40/5 (95 votes)
15 Sep 2009CPOL2 min read 94.7K   76   32
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)


Written By
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

 
GeneralMy vote of 4 Pin
datohugh30-Jan-13 18:55
datohugh30-Jan-13 18:55 
GeneralDoubt Relevant to mail concept Pin
msureshbabu151-Aug-12 6:02
msureshbabu151-Aug-12 6:02 
GeneralMy vote of 4 Pin
Vivek Johari4-Jan-11 6:34
Vivek Johari4-Jan-11 6:34 
GeneralMy vote of 4 Pin
Eric Xue (brokensnow)6-Sep-10 12:56
Eric Xue (brokensnow)6-Sep-10 12:56 
GeneralRe: My vote of 4 Pin
Sushant Joshi27-Sep-10 19:37
Sushant Joshi27-Sep-10 19:37 
GeneralGreat! Pin
Rajesh Govinda22-Oct-09 2:12
Rajesh Govinda22-Oct-09 2:12 
QuestionCode Please ? Pin
VasudeoPhadke21-Oct-09 22:02
VasudeoPhadke21-Oct-09 22:02 
GeneralMy vote of 1 Pin
Kirtan Gor21-Oct-09 18:42
Kirtan Gor21-Oct-09 18:42 
GeneralMy vote of 1 Pin
Kirtan Gor21-Oct-09 18:40
Kirtan Gor21-Oct-09 18:40 
GeneralRe: My vote of 1 Pin
MaheshMohan121-Oct-09 21:39
MaheshMohan121-Oct-09 21:39 
GeneralRe: My vote of 1 Pin
John_Bradley12@gmail.com27-Sep-10 2:01
John_Bradley12@gmail.com27-Sep-10 2:01 
GeneralMy vote of 1 PinPopular
R. Giskard Reventlov21-Oct-09 4:52
R. Giskard Reventlov21-Oct-09 4:52 
GeneralMy vote of 1 Pin
1.21 Gigawatts21-Oct-09 4:11
1.21 Gigawatts21-Oct-09 4:11 
GeneralRe: My vote of 1 Pin
Vicks123421-Oct-09 4:19
Vicks123421-Oct-09 4:19 
GeneralNot very constructive PinPopular
DaveyM6921-Oct-09 6:24
professionalDaveyM6921-Oct-09 6:24 
GeneralMy vote of 1 PinPopular
NormDroid21-Oct-09 3:53
professionalNormDroid21-Oct-09 3:53 
GeneralRe: My vote of 1 Pin
Vicks123421-Oct-09 4:00
Vicks123421-Oct-09 4:00 
GeneralMy vote of 2 Pin
Sacha Barber21-Oct-09 2:43
Sacha Barber21-Oct-09 2:43 
GeneralRe: My vote of 2 Pin
LakshmanPrasad21-Oct-09 3:28
LakshmanPrasad21-Oct-09 3:28 
GeneralRe: My vote of 2 Pin
DaveyM6921-Oct-09 6:27
professionalDaveyM6921-Oct-09 6:27 
GeneralRe: My vote of 2 Pin
Sacha Barber21-Oct-09 19:36
Sacha Barber21-Oct-09 19:36 
GeneralRe: My vote of 2 PinPopular
Sacha Barber21-Oct-09 19:39
Sacha Barber21-Oct-09 19:39 
GeneralRe: My vote of 2 Pin
Sushant Joshi21-Oct-09 22:20
Sushant Joshi21-Oct-09 22:20 
GeneralThank You! Pin
thund3rstruck16-Sep-09 9:21
thund3rstruck16-Sep-09 9:21 
GeneralRe: Thank You! Pin
Sushant Joshi24-Aug-10 19:21
Sushant Joshi24-Aug-10 19:21 

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

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