2

I try to send email using this code:

 public void Semail(string subject, string messageBody, string toAddress) { MailMessage mail = new MailMessage(); mail.To.Add(toAddress); //mail.To.Add("[email protected]"); mail.From = new MailAddress("[email protected]"); mail.Subject = subject; string Body = messageBody; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "sina.sharif.ir"; //Or Your SMTP Server Address smtp.Port = 587; smtp.Credentials = new System.Net.NetworkCredential ("[email protected]", "*******"); //Or your Smtp Email ID and Password smtp.EnableSsl = false; smtp.Send(mail); } 

But after executing i got this error:

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

Stack Trace :

[SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication required] System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) +2162008 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) +287 System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) +137 System.Net.Mail.SmtpClient.Send(MailMessage message) +2188821 Novitiate.fa.Register.btnLogin_Click(Object sender, EventArgs e) +2948 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707 

Best regards

11
  • Maybe smtp.EnableSsl=true;? Commented May 3, 2014 at 9:47
  • I change it to true but i got this error :The server can't support secure connection!!! Commented May 3, 2014 at 9:48
  • And that server is has an open port on 587? And you are not behind a proxy that needs authentication as well? Is a simple mailcient (outlook or thunderbird) capable of connecting? Commented May 3, 2014 at 9:51
  • You know we are a company that design a system for a university ,and we ask them that give us the information about their mail server and this gave us just a username and password and smtp server and a port ,i don't know any thing else about that!!! Commented May 3, 2014 at 10:02
  • 1
    I tried your SMTP on port 587 using telnet, it is working and responding, which means SMTP server is OK. Probably your credentials are NOT valid. You can try with telnet, see this link technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspx Commented May 3, 2014 at 10:33

3 Answers 3

2

Your gmail security preventing the message to access the gmail account. So,if you want to send the message you have to "turn off" the access for less secure app. When you do this you can easily send the message but, by risking your gmail security. So, it is upto you this definitely work.

link for changing security is given below

https://www.google.com/settings/security/lesssecureapps?pli=1

Sign up to request clarification or add additional context in comments.

Comments

0

Try this one if this helps:

MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress(Username); mail.To.Add(TxtEmail.Text); mail.Subject = Subject; mail.Body = Body; SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential(Username, Password); SmtpServer.EnableSsl = true; SmtpServer.Timeout = 20000; SmtpServer.Send(mail); 

2 Comments

what is the different ?
Difference is in SmtpServer.EnableSsl = true; it provides you a Secure Connection and you are getting the error for it.
0

Consider @Krunal Patil's answer there after login to your email account and act on the sent verification mail to grant access to your C# Application.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.