Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use all code but exception occured Following..
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

What I have tried:

i tried follwing code...
but it will give following exception
"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"
C#
try
            {
                System.Net.Mail.MailMessage Msg = new System.Net.Mail.MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtEmail.Text);
                // Recipient e-mail address.
               Msg.To.Add("administrator@aspdotnet-suresh.com");
            
                Msg.Subject = txtSubject.Text;
                Msg.Body = txtmsg.Text;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                //smtp.Credentials = new System.Net.NetworkCredential("yourgmailemail@gmail.com", "yourpassword");
                
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                //Msg = null;
                success.Text = "Thanks for Contact us";
                // Clear the textbox valuess
                txtname.Text = "";
                txtSubject.Text = "";
                txtmsg.Text = "";
                txtEmail.Text = "";
            }
            catch (Exception ex)
            {
              // Console.WriteLine("{0} Exception caught.", ex);
            }
Posted
Updated 2-May-16 23:53pm
v2
Comments
F-ES Sitecore 3-May-16 5:59am    
This is asked daily, please google before asking a question. You shouldn't be sending email through gmail, but if you must then google for code samples as there are lots out there.
Beginers*Devloper 3-May-16 6:10am    
i use sample code problem is that now host value be null
Philippe Mori 4-May-16 12:44pm    
By the way, it should be easy to test it in a client mail program. Once you have validated tht the information allows to send an email in an existing program, you know at least which parameters to specify.

1 solution

You don't provide the authentication the email server needs - your username and password!
Most email servers require login to prevent themselves being used as a spambot by unauthorized persons or "rogue code".

Here's the code I use: Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 
Comments
Beginers*Devloper 3-May-16 5:58am    
ok i will try this
Beginers*Devloper 3-May-16 6:08am    
not work still
OriginalGriff 3-May-16 6:23am    
Did you provide good login details?

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