How to send e-mail programmatically by using System.Web.Mail in Visual C# 2005 or in Visual C# .NET

Article translations Article translations
Article ID: 310273 - View products that this article applies to.
This article was previously published under Q310273
Expand all | Collapse all


This article demonstrates how to use the System.Web.Mail namespace to send an e-mail message in Microsoft Visual C# .NET.


  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Click Visual C# Projects, click the Console Application template, and then click OK. By default, Class1.cs is created.

    Note In Visual Studio 2005, click Visual C# instead of Visual C# Projects. By default, Program.cs is created.
  2. Add a reference to the System.Web.dll namespace. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the .NET tab, locate System.Web.dll, and then click Select.

      Note In visual Studio 2005, you do not have to click Select.
    3. Click OK in the Add References dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries that you selected, click Yes.
  3. In the code window, replace the whole code with:
    using System;
    using System.Web.Mail;
    namespace WebMail
        class Class1
            static void Main(string[] args)
                    MailMessage oMsg = new MailMessage();
                    // TODO: Replace with sender e-mail address.
                    oMsg.From = "";
                    // TODO: Replace with recipient e-mail address.
                    oMsg.To = "";
                    oMsg.Subject = "Send Using Web Mail";
                    // SEND IN HTML FORMAT (comment this line to send plain text).
                    oMsg.BodyFormat = MailFormat.Html;
                    // HTML Body (remove HTML tags for plain text).
                    oMsg.Body = "<HTML><BODY><B>Hello World!</B></BODY></HTML>";
                    // ADD AN ATTACHMENT.
                    // TODO: Replace with path to attachment.
                    String sFile = @"C:\temp\Hello.txt";  
                    MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
                    // TODO: Replace with the name of your remote SMTP server.
                    SmtpMail.SmtpServer = "MySMTPServer";
                    oMsg = null;
                    oAttch = null;
                catch (Exception e)
                    Console.WriteLine("{0} Exception caught.", e);
  4. Modify the code where you see "TODO".
  5. Press F5 to build and run the program.
  6. Verify that the e-mail message has been sent and received.


Article ID: 310273 - Last Review: December 11, 2006 - Revision: 3.6
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
kbhowto KB310273

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from