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

Send Mails from Windows Application

By , 15 Sep 2009
 

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
Member
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.

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberdatohugh30 Jan '13 - 18:55 
GeneralDoubt Relevant to mail conceptmembermsureshbabu151 Aug '12 - 6:02 
GeneralMy vote of 4memberVivek Johari4 Jan '11 - 6:34 
GeneralMy vote of 4memberEric Xue (brokensnow)6 Sep '10 - 12:56 
GeneralRe: My vote of 4memberSushant Joshi27 Sep '10 - 19:37 
GeneralGreat!memberRajesh Govinda22 Oct '09 - 2:12 
QuestionCode Please ?memberVasudeoPhadke21 Oct '09 - 22:02 
GeneralMy vote of 1memberKirtan Gor21 Oct '09 - 18:42 
GeneralMy vote of 1memberKirtan Gor21 Oct '09 - 18:40 
GeneralRe: My vote of 1memberMaheshMohan121 Oct '09 - 21:39 
GeneralRe: My vote of 1memberJohn_Bradley12@gmail.com27 Sep '10 - 2:01 
GeneralMy vote of 1memberdigital man21 Oct '09 - 4:52 
GeneralMy vote of 1member1.21 Gigawatts21 Oct '09 - 4:11 
GeneralRe: My vote of 1memberVicks123421 Oct '09 - 4:19 
GeneralNot very constructivemvpDaveyM6921 Oct '09 - 6:24 
GeneralMy vote of 1groupNorm .net21 Oct '09 - 3:53 
GeneralRe: My vote of 1memberVicks123421 Oct '09 - 4:00 
GeneralMy vote of 2mvpSacha Barber21 Oct '09 - 2:43 
GeneralRe: My vote of 2memberLakshmanPrasad21 Oct '09 - 3:28 
GeneralRe: My vote of 2mvpDaveyM6921 Oct '09 - 6:27 
GeneralRe: My vote of 2mvpSacha Barber21 Oct '09 - 19:36 
GeneralRe: My vote of 2mvpSacha Barber21 Oct '09 - 19:39 
GeneralRe: My vote of 2memberSushant Joshi21 Oct '09 - 22:20 
GeneralThank You!memberthund3rstruck16 Sep '09 - 9:21 
GeneralRe: Thank You!memberSushant Joshi24 Aug '10 - 19:21 
GeneralNice alternate approach!memberJonnBradley1215 Sep '09 - 17:53 
GeneralFar away from full SMTP clientmembermaxxnostra15 Sep '09 - 6:52 
GeneralRe: Far away from full SMTP clientmemberSushant Joshi15 Sep '09 - 17:41 
GeneralRe: Far away from full SMTP clientmembermaxxnostra16 Sep '09 - 7:25 
GeneralRe: Far away from full SMTP clientmemberSushant Joshi16 Sep '09 - 17:37 
General[Message Deleted]memberhuangli200921 Oct '09 - 2:55 
GeneralRe: Far away from full SMTP clientmemberLakshmanPrasad21 Oct '09 - 3:20 

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

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