Introduction:

In this article we will learn how to send a mail in asp.net using web from in c sharp code.


Description:


To implement mail functionality in asp.net first we need to add following reference to our application System.Net.Mail and System.Net namespace .

In The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.

 

What is SMTP? 

 

Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol used in sending and receiving e-mail. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another. The messages can then be retrieved with an e-mail client using either POP or IMAP.

 

SMTP Class Properties:


Host: Server URL for SMTP (smtp.gmail.com).

EnableSsl: Check your host accepts SSL Connections (True or False).

Port: Port Number of the SMTP server (25).

Credentials: Valid login credentials for the SMTP server (the email address and password).

UseDefaultCredentials: When we set to True in UseDefaultCredentials then that specifies to allow authentication based on the credentials of the account used to send emails.

 

Let’s Simple Example of send mail in Asp.Net.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
namespace Email
{
   public partial class WebForm1 : System.Web.UI.Page
   { 
       protected void btnSendMail_Click(object sender, EventArgs e)
       {
          try
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("xyz@gmail.com");
                msg.To.Add("abc@gmail.com");
                msg.Subject = "Testing emaildelivery";
                msg.Body = "Testing";
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true;
                NetworkCredential credentials =
                new NtworkCredential("zyx@gmail.com", "Password here");
                client.Credentials =credentials;
                client.Send(msg);
                Response.Write("We Will Contact you as soon as Possible!!! ");
            }
            catch
            {
                Response.Write("please try again letter!!! ");
            }
 
            finally { }
 
       }
   }
}


I hope it will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.