Click here to Skip to main content
15,897,371 members
Articles / Web Development / ASP.NET

SendMail 101 - How to send e-mails over SMTP (C# and ASP.NET)

Rate me:
Please Sign up or sign in to vote.
4.23/5 (24 votes)
24 Nov 20022 min read 315.2K   10.1K   103  
This sample shows you how to send e-mails over SMTP using the Mail-Class in C# and ASP.NET.
/*-----------------------------------------------------------------------------------------------------------
 * Author           :Sujith John Thomas
 * Project          :SMTP Mail System Using ASP.NET 2.0
 * Module Name      :Send Mail
 * Dated            :16/07/2007
 * Modified on      :
 * ----------------------------------------------------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;

public partial class SendMail : System.Web.UI.Page
{
    #region DECLARATIONS
    private string _mMailServer;
    private string _mTo;
    private string _mFrom;
    private string _mMessage;
    private string _mSubject;
    private int _mPort;
    private MailMessage objMessage;
    #endregion
    #region EVENTS
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        Send();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Clear();
    }
    #endregion
    #region METHODS
    //----------------------------------------------------------
    //Method to show the message 
    //----------------------------------------------------------
    private void Message(string strMessage)
    {
        Label lblMsg = new Label();
        lblMsg.Text = strMessage;
        phMessage.Controls.Add(lblMsg);
    }
    //----------------------------------------------------------
    //Method to send the mail.
    //----------------------------------------------------------
    private void Send()
    {    
        //Assigning value to the variables
        _mTo = txtTo.Text.Trim();
        _mFrom =txtFrom.Text.Trim();
        _mSubject = txtSubject.Text.Trim();
        _mMessage = txtBody.Text.Trim();

        //Getting the mail server ip from the web.config file
        _mMailServer = ConfigurationManager.AppSettings["MailServer"].ToString();
        //Getting the mail server port from the web.config file.
        _mPort = Int32.Parse(ConfigurationManager.AppSettings["MailServerPort"].ToString());

        try
        {   
            //Passing parameters to the Mail Message object.
            objMessage = new MailMessage(_mFrom, _mTo, _mSubject, _mMessage);
            //Instantiating Attachement object
            Attachment attach = new Attachment(fileAttach.PostedFile.FileName);
            //Adding attachment to the message.
            objMessage.Attachments.Add(attach);
            //Instantiating the SmtpClient
            SmtpClient objSmtpClient = new SmtpClient(_mMailServer, _mPort);
            //Setting default credentials to true
            objSmtpClient.UseDefaultCredentials = true;


            //Sending the mail. 
            objSmtpClient.Send(objMessage);
            Message("Mail has been sent successfully.");
            //Resetting the form
            Clear();
        }
        catch (FormatException ex)
        {

            Message("Format Exception:" + ex.Message);
        }
        catch (SmtpException ex)
        {

            Message("SMTP Exception:" + ex.Message);
        }
        catch (Exception ex)
        {

            Message("General Exception:" + ex.Message);

        }

    }
    //---------------------------------------------------------
    //Method for clearing the fields.
    //---------------------------------------------------------
    private void Clear()
    {
        txtTo.Text = string.Empty;
        txtFrom.Text = string.Empty;
        txtSubject.Text = string.Empty;
        txtBody.Text = string.Empty;
    }
    #endregion

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
1978-I was born
1989-My parents (++me, ++myBrother) moved to Germany
1990-I've got my first Computer (Amstrad CPC-464)
1991-I've bought a Commodore C64 by myself
1992-I've bought my first PC (486sx25)
1993-My first Website (static HTML) was ready
1993-PHP, ASP, SQL, C++, JavaScript, Python
2001-eightfour goes csharp

Comments and Discussions