Free Web Development Online Tutorials, Learn to Code
How to send email in Asp.net C# Web Application, asp.net code to send email in c#

Email sending in Asp.net C#

Here you learn how to send email in asp.net web application, step by step email sending application tutorial for beginners.

email sending in asp.net c#

How to send email in Asp.net

Here are the steps to create an email sending application in asp.net c#.

  1. Create a web form, where user can type their email id, subject line, email body content etc.
  2. Set your all SMTP details like SMTP server, username, password, port, from email id, reply to email id etc. in web config fie, so you can change them any time.
  3. Create an instance of MailMessage class, and send all properties correctly.
  4. Create an instance of SmtpClient class, and execute the send method with MailMessage object

Asp.net send email form design

Email
Message

Step 1: Create a form like above form, You can add all other fields like Subject, CC, BCC etc. now we have removed all those fields, so you can understand the core mail sending functionality

For each input text box control, you need to set right validation control to make sure the input data is in correct format and length.

<table>
    <tr>
        <td>Subject:</td>
        <td>
            <asp:TextBox runat="server" ID="txtSubject"
            Width="319px"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Email:</td>
        <td>
            <asp:TextBox runat="server" ID="txtEmail"
            Width="320px"></asp:TextBox>
            <div>
                <asp:RegularExpressionValidator runat="server" 
                ID="rev"
                ControlToValidate="txtEmail"
                ErrorMessage="Provide valid email!" 
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
                </asp:RegularExpressionValidator>
            </div>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <div>Description</div>
            <textarea runat="server" id="txtDescrption"></textarea>
        </td>
    </tr>
    <tr>
        <td colspan="2" style="text-align: center;">
            <asp:Button runat="server" ID="btnSendEmail"
            Text="Send Email" OnClick="btnSendEmail_Click" />
        </td>
    </tr>
</table>
Code behind Button Click Method

When user click email send button, the btnSendEmail_Click method in code behind will be executed, there you can capture all user input, and call the Send method of SmtpClient

protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        string _emailId = txtEmail.Text;
        string _emailBody = txtDescrption.Value;
        string _emailSubject = txtSubject.Text;

        WTRMail.SendEmail(_emailId, _emailSubject, _emailBody);
    }

Now you need to write a utility class, where all email sending method related functionalities will be written; so here i am creating a class with name "WTRMail".

public class WTRMail
{
    
        public string MTo { get; set; }
        public string MSubject { get; set; }
        public string MBody { get; set; }

        public static void SendEmail(string emailId, string subject, string emailBody)
        {
           MTo= emailId;
           MSubject= subject;
           MBody= emailBody;     
           SendETMail();           
        }
}

Set SMTP details in web.config

Step 2: Add your SMTP details in web.config file, it will be good if keep configuration details in a separate File, and read the file using configuration framework

<SmtpServer>my-SMTP.serverName.IPAddress</SmtpServer>
<SmtpUserName>smtpUserName@mydomain.com</SmtpUserName>
<SmtpPassword>mySMTPPassword</SmtpPassword>
<SMTPPort>25</SMTPPort>
<DisplayName>WebTrainingRoom.com</DisplayName>
<FromEmail>support@webtrainingroom.com</FromEmail>
<ReplyTo>webtrainingroom@gmail.com</ReplyTo>
Set MailMessage in C# code for sending email

Step 3: Now create a utility class with some name, here we have created WTRMail, inside that create a method called SendMail() with two object MailMessage and SmtpClient

public class WTRMail
    {
        MailMessage objMail = new MailMessage(MFrom, MTo, MSubject, MBody);
        //Default setting
        objMail.Priority = MailPriority.Normal;
        objMail.IsBodyHtml = this.IsHtmlBody;
        objMail.ReplyTo = new MailAddress(this.ReplyTo);
    }

Read SMTP info from web.config

Step 4: Read all your SMTP configuration information from web.config or configuration framework object

public class WTRMail            
{
string _SmtpServer= ConfigurationManager.AppSettings["SmtpServer"];
string _SmtpUserName= ConfigurationManager.AppSettings["SmtpUserName"];
string _SmtpPassword= ConfigurationManager.AppSettings["SmtpPassword"];
string _SMTPPort= ConfigurationManager.AppSettings["SMTPPort"];           
           
    private bool SendETMail()
    {
        SmtpClient objSmtp = new SmtpClient();
            
        objSmtp.EnableSsl = false ;
        objSmtp.Host = _SmtpServer;
        objSmtp.Port = _SMTPPort;
        objSmtp.Credentials = new System.Net.NetworkCredential(_SmtpUserName, _SmtpPassword);
        objSmtp.Send(objMail);
    }            
}

In above code you can see how we read the SMTP information from web.config file, then created a new instance of SmtpClient class, then set server, port, credentials and finally executed the send method

You also may be interested to know how to Send Email in Asp.net MVC Application


Comment
Name
Email
Website
Subscribe
 
learn asp.net

hire remote asp.net developer
learn aspnet mvc
Send Email using Asp.Net
Asp.net Interview Questions Answers

Asp.net SMTP Example
Asp.Net C# Examples | Join Asp.Net Course | Learn Asp.net MVC | Asp.net Core Tutorial