Reputation: 7301
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
Upvotes: 2
Views: 3547
Reputation: 1
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.
Upvotes: 0
Reputation: 21
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
Upvotes: 2
Reputation: 3676
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);
Upvotes: 0