邮件与网络凭证在Windows作为真正的发送表单无法正常工作 [英] mail sending with network credential as true in windows form not working

查看:111
本文介绍了邮件与网络凭证在Windows作为真正的发送表单无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的事情在C#中的窗口发送邮件的应用程序使用application.I SMTP服务器,但我不希望设置的网络凭据。所以,我把它设置为true.but我得到错误。

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.

SMTP服务器要求安全连接或客户端未
  验证。服务器响应为:5.5.1需要验证。
  了解更多

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

下面是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");

在这里我设置了主机gmail.com,我需要使用所有的电子邮件服务providers.So我怎么可以设置主机​​和端口来发送和接收邮件?

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?

推荐答案

Gmail使用端口587

gmail uses port 587

oClient.Port = 587;

您可能还需要设置 UseDefaultCredentials 设置为false,显式声明的用户名和密码。通过声明这是真的,你想使用Windows凭据登录到您的Gmail帐户。

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");

此外,在Gmail中的安全性,则需要允许安全性较低的应用程序。

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

最后,你需要改变你的Mail.To的填充方式:

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

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

这篇关于邮件与网络凭证在Windows作为真正的发送表单无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆