无法使用SmtpClient发送邮件 [英] Can't send mail using SmtpClient

查看:48
本文介绍了无法使用SmtpClient发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SmtpClient类发送邮件,但是它不起作用.代码:

I want to send mail using SmtpClient class, but it not work. Code:

SmtpClient smtpClient = new SmtpClient();

smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(obMailSetting.UserName, obMailSetting.Password);

smtpClient.Host = obMailSetting.HostMail;
smtpClient.Port = obMailSetting.Port;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = obMailSetting.Connect_Security;//true
//smtpClient.UseDefaultCredentials = true;//It would work if i uncomment this line
smtpClient.Send(email);

它引发异常:

SMTP服务器需要安全连接,或者客户端未通过身份验证.服务器响应是:5.7.1客户端未通过身份验证

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

我确定用户名和密码正确.我的代码有什么问题吗?谢谢.

I'm sure that username and password is correct. Is there any problem in my code? Thanks.

推荐答案

服务器响应 5.7.1客户端未通过身份验证,但前提是您未设置 UseDefaultCredentials 改为 true .这表明您使用的 NetworkCredential 实际上是错误的.

The server responds with 5.7.1 Client was not authenticated but only if you do not set UseDefaultCredentials to true. This indicates that the NetworkCredential that you are using is in fact wrong.

用户名或密码错误,或者您需要指定域?您可以使用其他构造函数来指定域:

Either the user name or password is wrong or perhaps you need to specify a domain? You can use another constructor to specify the domain:

new NetworkCredential("MyUserName", "MyPassword", "MyDomain");

或者您指定的用户没有必要的权限在SMTP服务器上发送邮件,但是我希望另一个服务器响应.

Or perhaps the user that you specify does not have the necessary rights to send mail on the SMTP server but then I would expect another server response.

这篇关于无法使用SmtpClient发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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