Sending a mail through ASP.Net C#

It is very easy to send email form asp.net or C# Application.

Step 1.

Make a Class (MailSettings.cs)


 

Include these files in that


 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Mail;

using System.Web;

using System.Web.Configuration;

using System.Net.Configuration;

using System.Configuration;


 

Declare these variables in that


 

static
string userName, password, smtpClientHost;

static
int smtpClientPort;

static
bool smtpClientEnableSSL;


 

Create Two functions in that


 

First Function


 

private
static
void setParameters()

{

    try

{


Configuration config = WebConfigurationManager.OpenWebConfiguration("~\\Web.config");

    MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

        userName = settings.Smtp.Network.UserName;

password = settings.Smtp.Network.Password;

smtpClientHost = settings.Smtp.Network.Host;

smtpClientPort = settings.Smtp.Network.Port;

smtpClientEnableSSL = true;

    }


catch (Exception ex)

{

    throw ex;

    }

}


 

Second Function


 

public
void SendEmail(string mailto string mailFrom, string mailSubject, string mailBody, string mailAttachment)

{

    try

{

    setParameters();


MailMessage msg = new
MailMessage();

msg.From = new
MailAddress(mailFrom);

msg.To.Add(mailto);

msg.Subject = mailSubject;

msg.Body = mailBody;

msg.IsBodyHtml = true;

msg.Attachments.Add(new
Attachment(mailAttachment));

        SmtpClient smtpclient = new
SmtpClient();

smtpclient.EnableSsl = smtpClientEnableSSL;

smtpclient.Host = smtpClientHost;

smtpclient.Port = smtpClientPort;

smtpclient.Send(msg);

    }


catch (Exception ex)

{

    throw ex;

    }

}


 

For sending mail without attachment create third function as second function and make the changes as given below.


 

  1. Remove the last parameter "string mailAttachment";
  2. Remove line "msg.Attachments.Add(new
    Attachment(mailAttachment))";


 

Step 2

Add the given code in the web.config file.


 

<system.net>

    <mailSettings>

        <smtp>

            <network
host="HOST FOR GMAIL"
port="PROT FOR GMAIL"
defaultCredentials="false"
userName="YOUR GMAIL ID"
password="PASSWORD FOR GMAIL" />

        </smtp>

    </mailSettings>

</system.net>


 

Change the code in CAPITAL LETTERS in your own settings.

This code will work only for the gmail email accounts.


 

Step 3

Create in design page as below.


 

Name        :    Name of Sender    <TextBox>

Email        :    Sender's Email ID    <TextBox>

Subject    :    Subject for email    <TextBox>

Body        :    Body for email    <TextArea>

Attachment    :    File Path

        Send & Cancel Buttons


 

Now in the code page, in send button's click event type the code below.

Incude files

using System.Threading;


 

protected
void SendMail_Click(object sender, EventArgs e)

{

    try

{

    string body = MailBodytext();


Thread t = new
Thread(() => mailsend("Receiver's Email" , TxtEmail.Text , txtSubject.Text, body , filepath));

t.Start();

Response.Redirect("Home.aspx");

    }


catch (Exception ex)

{

    throw ex;

    }

}


 

You can also get user's email dynamically from either Hidden field, database or web.config file.


 

protected
void mailsend(string emailto, string emailfrom, string subject, string body)

{

    try

{

        MailSettings mail = new
MailSettings();


bool success = mail.SendEmail(emailto, emailfrom, subject, body, filepath);

    }


catch (Exception ex)

{

    return;

    }

}

private
string MailBodytext()

{


string Body = "";

Body += "<b>Mail By : </b>" + TxtEmail.Text + "<br/>";

Body += "<b>Description : </b><br/>" + txtBody.Text;

    return Body;

}


 


 

Search This Blog

Link Within Related Posts Plugin for WordPress, Blogger...