mail sending with network credential as true in windows form not working

微笑、不失礼 提交于 2019-11-26 08:52:47

问题


I want to do an application for sending emails in C# windows application.I used smtp server,but I don\'t want to set the network credentials. So I set it as true.but am getting error.

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

Here is the code:

SmtpClient oClient = new SmtpClient();
oClient.Host = \"smtp.gmail.com\";
oClient.Port = 25;
oClient.UseDefaultCredentials = true;
oClient.Credentials = new System.Net.NetworkCredential();
oClient.EnableSsl = true;
MailMessage oMail = new MailMessage();
oMail.To.Add(txtTo.Text.Trim());
oMail.From = new MailAddress(\"testmail@gmail.com\");
oMail.Subject = txtSubject.Text;
oMail.Body = txtBody.Text;
oMail.IsBodyHtml = true;
oClient.Send(oMail);
MessageBox.Show(\"Mail Send\");

here I set the host as gmail.com,I need to send and receive mails using all email service providers.So how can I set the host and port?


回答1:


gmail uses port 587

oClient.Port = 587;

You may also want to set UseDefaultCredentials to false and explicitly declare username and password. By declaring it to be true, you are trying to log into your gmail account using your windows credentials.

oClient.UseDefaultCredentials = false;
oClient.Credentials = new NetworkCredential("email@gmail.com", "password");

Also, in gmail security, you will need to allow Less Secure Applications.

Finally, you need to change how your Mail.To is populated:

oMail.To.Add(new MailAddress(txtTo.Text.Trim()));



回答2:


Google may block sign in attempts from some apps or devices when you try to login from some app. You need to go to security settings at your gmail and enable less secure apps . So that you will be able to login from all apps. Go to Allow less secure apps and choose Allow to let less secure apps access your Google account. Have a look at this link may useful.



来源:https://stackoverflow.com/questions/32475832/mail-sending-with-network-credential-as-true-in-windows-form-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!