在 Windows 窗体中使用网络凭据发送邮件为 true 不起作用 [英] mail sending with network credential as true in windows form not working

查看:25
本文介绍了在 Windows 窗体中使用网络凭据发送邮件为 true 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 C# windows 应用程序中做一个发送电子邮件的应用程序.我使用 smtp 服务器,但我不想设置网络凭据.所以我将其设置为 true.但出现错误.

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

代码如下:

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,需要使用所有邮箱服务商收发邮件,请问如何设置主机和端口?

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 并明确声明用户名和密码.通过声明它为 true,您正在尝试使用您的 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 窗体中使用网络凭据发送邮件为 true 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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