How to send email in C# from asp.net. The code is not workinfg

//Error - System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

using System;
using System.Configuration;
using System.Data;
using System.Web.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        int port = 587;
        System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage();
        System.Net.Mail.SmtpClient objSmtp = new System.Net.Mail.SmtpClient("smtp.gmail.com",port);


        try
        {
           
            objMail.To.Add(txtTo.Text);
            objMail.From = new System.Net.Mail.MailAddress("abc@gmail.com");
            //This code of line is used for specifying the subject line.
            objMail.Subject = txtSubject.Text;
            objMail.IsBodyHtml = true;
            objMail.Body = txtContent.Value;


            objSmtp.UseDefaultCredentials = false;
            objSmtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "******");
            objSmtp.EnableSsl = true;

            objSmtp.Send(objMail);
            Response.Write("<script>alert('Email sent');</script>");
        }
        catch (Exception ess)
        {
        }

           
 
    }
}
searchsanjaysharmaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the_billCommented:
hi,

If you've tried sending multiple times, google might have placed a temp block, which puts a "word verification" (captcha). Sign in via web interface, check your credentials and fill in captcha. Has that helped?
0
pschramaCommented:
According to http://mail.google.com/support/bin/answer.py?hl=en&answer=13287, the port for SSL is 465, try that instead.
0
AshokSr. Software EngineerCommented:
using System.Net.Mail;

protected void btnSend_Click(object sender, EventArgs e)
{
  MailMessage mail = new MailMessage();
  mail.To.Add("SanjaySharma@gmail.com");
  mail.To.Add("Ashok122@yahoo.com");
  mail.From = new MailAddress("abc@gmail.com");
  mail.Subject = "Email using Gmail";

  string Body = "Hi, this mail is to test sending mail"+
                "using Gmail in ASP.NET";
  mail.Body = Body;

  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient();
  smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
  smtp.Port = 587;
  smtp.Credentials = new System.Net.NetworkCredential
       ("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
  smtp.EnableSsl = true;
  smtp.Send(mail);
}

If this does not help, see http://csharpdotnetfreak.blogspot.com/2009/08/send-email-using-gmail-in-aspnet.html

HTH
Ashok
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
searchsanjaysharmaAuthor Commented:
Thanks excellent
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.