Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

while sending email from asp.net page using gmail smtp the following error occur :-

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

the code i have used for sending email is as follow:
protected void btSubmit_Click(object sender, EventArgs e)
    {
        try
        {

            SmtpClient smtpClient = new SmtpClient();
            MailMessage objMail = new MailMessage();
            //From Address will be assigned from the e-mail specified in the From TextField You can also give the code given below.
            MailAddress objMail_fromaddress = new MailAddress(txtMail.Text);
            MailAddress objMail_toaddress = new MailAddress("mymail@gmail.com");
            objMail.IsBodyHtml = true;
            //Assigning From address to the MailMessage class
            objMail.From = objMail_fromaddress;
            //Assigning To address to the MailMessage class as a collection
            objMail.To.Add(objMail_toaddress);
            objMail.Subject = "Feedback from" + txtName.Text + " to us from Contact us Form";
            objMail.Body = "Message from" + txtName.Text + "as comments:-" + txtComments.Text + "<br><br><br>" + "Address:" + txtAddress.Text + "<br>Mobile no:" + txtTel.Text;
            objMail.Priority = MailPriority.High;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Credentials = new  System.Net.NetworkCredential("mymail@gmail.com", "1239807643");
            smtpClient.EnableSsl = true;
            smtpClient.Send(objMail);
            Label1.Visible = true;
            Label1.Text = "your feedback is submitted successfully.";
        }
        catch (Exception ex)
        {

            Label1.Visible = true;
            Label1.Text = "something went wrong!, Please Try Again." + ex.Message;
        }


[edit]Cod block added - OriginalGriff[/edit]
Posted
Updated 15-Jan-11 22:42pm
v2

Accessing gmail requires
smtpClient.Port = 587


Cheers
 
Share this answer
 
Comments
Espen Harlinn 16-Jan-11 7:04am    
5+ Nicely spotted - and here is a link:http://office.microsoft.com/en-us/outlook-help/use-outlook-with-google-gmail-HA001148898.aspx
you are using default SMTP port

but gmail is working on 587

use
smtpClient.Port = 587
before
smtpClient.Send(objMail);


one more thing
MailAddress objMail_fromaddress = new MailAddress(txtMail.Text); // is wrong it should be your gmail user name.


and

MailAddress objMail_toaddress = new MailAddress("mymail@gmail.com");  // it seems like you are sending mail to yourself



use this code

//MailAddress objMail_fromaddress = new MailAddress(txtMail.Text);
MailAddress objMail_fromaddress = new MailAddress(mymail@gmail.com);
//MailAddress objMail_toaddress = new MailAddress("mymail@gmail.com");
MailAddress objMail_toaddress = new MailAddress(txtMail.Text);


good luck :cool:
 
Share this answer
 
v3
Two possibilities I can see:
1) Your user name and / or password is incorrect in the smtpClient.Credentials line.
2) You are trying to use a secure connection and it is not supported.

Simplest: comment out the line
ss
smtpClient.EnableSsl = true;
And see if the problem goes away.
 
Share this answer
 
Comments
Balwant.mnd 16-Jan-11 4:58am    
no these are correct

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