Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;

public partial class contact : System.Web.UI.Page

    protected void SendEmail()
            MailMessage message = new MailMessage();
            MailAddress Sender = new MailAddress(ConfigurationManager.AppSettings["smtpUser"], "Inquiry");
            MailAddress receiver = new MailAddress("", "Inquiry");
            SmtpClient smtp = new SmtpClient()
                Host = ConfigurationManager.AppSettings["smtpServer"],
                Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]),
                EnableSsl = true,
                UseDefaultCredentials = false,
                Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"])

            message.From = Sender;

            message.Subject = "New Inquiry @ " + txtname.Text;

            string body = "From: " + txtname.Text + " <br />";
            body += "Mobile Phone: " + txtmobile.Text + " <br />";
            body += "Message: " + txtmsg.Text + " <br />";

            message.Body = body;
            message.IsBodyHtml = true;
        catch (Exception ex)


    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)

        ScriptManager.RegisterStartupScript(this, GetType(), "alertmsg", "alert('Thank you for Inquiry!Our team contact you soon');", true);

What I have tried:

what is the problem in it
mail does not send
please help to solve this problem
Updated 10-May-16 20:52pm
Michael_Davies 11-May-16 2:11am    
What is the error message?
Member 11811231 11-May-16 2:15am    
no error on it but email cant recive
Member 11811231 11-May-16 2:22am    
ya bro i'm getting error something like that

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Michael_Davies 11-May-16 2:35am    
What are the values of smtpServer and smtpPort.
Jawad Ahmed Tanoli 11-May-16 2:21am    
Check in your spam

1 solution

try below snippet, here I am sending mail using Gmail account
put your code always I TRY..CATCH block to get exact error
MailMessage mail = new MailMessage();

//put mail from Email
 mail.From = new MailAddress("");

//put mail to Email
 mail.To.Add(new MailAddress(""));

//you can add blank carbon copy mails here
//mail.Bcc.Add(new MailAddress("YOUR BCC EMAIL"));

//Get an attachment in attachment variable class
Attachment att = new Attachment("D:\\test.txt");

//add attachment to email

//Put email subject
 mail.Subject = "Send Email with Gmail using ASP.NET";

//Put email body
 string Body = "Put your Email Body TEXT here";

// Here you can put HTML string if you want email to be sent in HTML format.   
 mail.Body = Body;
 mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential("", "PASSWORD");

//keep ssl true if you have to send email with secured socket layer
smtp.EnableSsl = false;


Exception points
There are so many factor that may affects your email sending code, here is list,
1. Firewall block port 587 (This is port for smtp gmail server)
2. Internet connection is not available on machine or internet connection has face some obstacles while pinging
3. Due to some typo mistake UserName and password may be get wrong
4. Enable SSL is block email sending
5. POP settings for smtp server in your Gmail account is disabled
Share this answer
Maciej Los 11-May-16 3:03am    
koolprasad2003 11-May-16 3:38am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900