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



The information in this article applies to:

  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

This article was previously published under Q310273

SUMMARY

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

MORE INFORMATION

  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)
            {
                try 
                {
                    MailMessage oMsg = new MailMessage();
                    // TODO: Replace with sender e-mail address.
                    oMsg.From = "sender@somewhere.com";
                    // TODO: Replace with recipient e-mail address.
                    oMsg.To = "recipient@somewhere.com";
                    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);
      
                    oMsg.Attachments.Add(oAttch);
    
                    // TODO: Replace with the name of your remote SMTP server.
                    SmtpMail.SmtpServer = "MySMTPServer";
                    SmtpMail.Send(oMsg);
    
                    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.

Modification Type:MinorLast Reviewed:10/4/2006
Keywords:kbhowto KB310273 kbAudDeveloper