Send email using web application like Contact Us page in ASP.Net

Last Reply on Jul 21, 2016 02:35 AM By dharmendr

Posted on Jul 20, 2016 03:52 AM

how can the user send their requirement via email using my web application in asp.net...user must send mail to the mail account which associated with a Gmail account in my site......

Dear i have a site ...on my site, there is three textbox 

1."From" in this box user need to write his mail id.

2."Subject" what he/she is going to send me 

3. "Message Box"

4.And a button called "Send"

user type their mail id , subject and message and then user click on the button and the mail will come to my email is which is attached to my site

 

Posted on Jul 21, 2016 02:35 AM

Hi Oyon,

Refer the below article for sending email.

Send email with attachment in ASP.Net using C# VB.Net

  • Use RegularExpressionValidator and RequiredFieldValidator Validator to email validation to textbox where you will accept User Email Id.
  • Same for Subject text box with RequiredFieldValidator Validator to check that subject cannt be blank.
  • Same for Message Box Textbox with RequiredFieldValidator Validator to check that Message as it cannt be blank.
  • For Send Button click you subject need to set the values of this textboxes with your Email account details which you will you use to send Emails from Your site.

Code

public void SendEmail(string fromEmailAddress, string subject, string body)
{
    using (MailMessage mm = new MailMessage())
    {
        // To set Email Id Which user has entered. 
        mm.From = new MailAddress(fromEmailAddress);

        // It is your email which you used for receive email Id
        mm.To.Add(new MailAddress("Youremail@gmail.com"));

        // Subject Which you fill in Subject Textbox
        mm.Subject = subject;

        // Body Which you fill in Message box Textbox
        mm.Body = body;

        mm.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();

        // your Accont details Which you are going to Send Email from Application            
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        System.Net.NetworkCredential networkCred = new System.Net.NetworkCredential();
        // Account Details Username And password
        networkCred.UserName = "Username@gmail.com";
        networkCred.Password = "your gmail password";
        smtp.UseDefaultCredentials = true;
        //
        smtp.Credentials = networkCred;

        smtp.Port = 587; //Account Port Number For Gmail its 587
        smtp.Send(mm);
    }
}

protected void ButtonClick(object sender, EventArgs e)
{
    //Calling SendEmail on button click.
    SendEmail(txtFrom.Text.Trim(), txtSubject.Text.Trim(), txtBody.Text.Trim());
}

Hope this will help you.