简体   繁体   English

System.Net.Mail.SmtpClient 在发送电子邮件时使用什么类型的身份验证?

[英]What type of authentication is System.Net.Mail.SmtpClient using when sending emails?

Hopefully this question makes sense.希望这个问题是有道理的。 I have some C# code that sends an email message given the username, password, and domain.我有一些 C# 代码,用于发送给定用户名、密码和域的电子邮件。 How do I know what type of authentication it's using?我怎么知道它使用的是什么类型的身份验证?

When I looked at the MSDN page for SmtpClient.Credentials Property it said that if you use basic authentication then credentials are sent over as plain text.当我查看 SmtpClient.Credentials 属性的 MSDN 页面时,它说如果您使用基本身份验证,则凭据将作为纯文本发送。 Is the below code using basic authentication?以下代码是否使用基本身份验证? MSDN page: https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.credentials(v=vs.110).aspx MSDN 页面: https : //msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.credentials(v=vs.110).aspx

SmtpClient smtpClient = new SmtpClient();
NetworkCredential cred = new NetworkCredential("username", "password"); 
MailMessage message = new MailMessage(); 
MailAddress fromEmail = new MailAddress("from@yourdomain.com"); 

smtpClient.Host = "mail.mydomain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = cred;

message.From = fromEmail;
message.Subject = "my subject";

message.IsBodyHtml = true;
message.Body = "hello world!";
message.To.Add("toEmail@anything.com"); 

smtpClient.Send(message);

Typically SmtpClient will alway pick the "best" available authentication method (the SMTP server will tell the in the answer to the EHLO command). 通常, SmtpClient将始终选择“最佳”可用的身份验证方法(SMTP服务器将在EHLO命令的答案中告知)。 So we can't really answer without knowing what the server actually supports. 因此,如果不知道服务器实际支持什么,我们就无法真正回答。

SmtpClient can choose from following methods: Negotiate, NTLM, Digest and Login SmtpClient可以选择以下方法:Negotiate,NTLM,Digest和Login

Here's some interesting reading about how to force SmtpClient to use a specific authentication method (slightly hack-ish btw.): http://blogs.msdn.com/b/knom/archive/2008/04/16/hacking-system-net-mail-smtpclient.aspx 这里有一些有趣的解释,关于如何强制SmtpClient使用特定的身份验证方法(稍微有些hack-ish btw。): http//blogs.msdn.com/b/knom/archive/2008/04/16/hacking-system-净邮件smtpclient.aspx

This method will use basic authentification and will send the code via unsecure channel. 此方法将使用基本身份验证,并将通过不安全的通道发送代码。 This is because: 这是因为:

The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel , then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. SmtpClient类仅支持RFC 3207中定义的基于传输层安全性的SMTP服务扩展。 在此模式下,SMTP会话从未加密的通道开始 ,然后客户端向服务器发出STARTTLS命令以切换到使用SSL进行安全通信。 See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information. 有关详细信息,请参阅Internet工程任务组(IETF)发布的RFC 3207。

You can use the SSL for such situations: 您可以在以下情况下使用SSL:

The EnableSsl property specifies whether SSL is used to access the specified SMTP mail server. EnableSsl属性指定是否使用SSL访问指定的SMTP邮件服务器。

Also please note that 另请注意

You can use ClientCertificates to specify which client certificates should be used to establish the SSL connection. 您可以使用ClientCertificates指定应使用哪些客户端证书来建立SSL连接。 The ServerCertificateValidationCallback allows you to reject the certificate provided by the SMTP server. ServerCertificateValidationCallback允许您拒绝SMTP服务器提供的证书。 The SecurityProtocol property allows you to specify the version of the SSL protocol to use. SecurityProtocol属性允许您指定要使用的SSL协议的版本。

This is an old thread, but I don't see my problem addressed here.这是一个旧线程,但我没有看到我的问题在这里得到解决。 I have a Windows Server using the soon-to-be-deprecated System.Net.Mail.SmtpClient.我有一个使用即将弃用的 System.Net.Mail.SmtpClient 的 Windows 服务器。 Because my emails are going through a load balancer which does not support AUTH LOGIN (multi-line authentication), I set my postfix servers to use AUTH PLAIN (single-line).由于我的电子邮件通过不支持 AUTH LOGIN(多行身份验证)的负载平衡器,我将我的 postfix 服务器设置为使用 AUTH PLAIN(单行)。 Can I get SmtpClient to send (after it successfully sets up STARTTLS) to send AUTH PLAIN???我可以让 SmtpClient 发送(在它成功设置 STARTTLS 之后)来发送 AUTH PLAIN 吗??? It does not seem to be doing so.它似乎没有这样做。 Can it???它可以???

Maybe my question should be in a new thread, but I see comments in this thread about the SMTP server sending back AUTH options for the client to pick the best.也许我的问题应该在一个新线程中,但我在这个线程中看到关于 SMTP 服务器发回 AUTH 选项供客户端选择最佳选项的评论。 And my SMTP server is sending back AUTH PLAIN in the list (as best/only), but the client does not seem to be picking it.我的 SMTP 服务器正在发回列表中的 AUTH PLAIN(最好/唯一),但客户端似乎没有选择它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用System.Net.Mail.SmtpClient将电子邮件发送到通讯组列表 - Sending email to a distribution list using System.Net.Mail.SmtpClient 如何在Xamarin中使用System.Net.Mail.SmtpClient发送邮件 - How to send a mail in Xamarin using System.Net.Mail.SmtpClient 使用System.Net.Mail.SmtpClient发送的电子邮件的字体大小 - Font size for an email sent using System.Net.Mail.SmtpClient System.Net.Mail.SmtpClient 在 4.7 中过时了吗? - Is System.Net.Mail.SmtpClient obsolete in 4.7? 第三方与System.Net.Mail.SmtpClient - Third-party vs System.Net.Mail.SmtpClient 发送到 System.Net.Mail.SmtpClient 等文件夹 - Send to folder like System.Net.Mail.SmtpClient 如何使用TLS和System.Net.Mail.SMTPCLient连接到googlemail.com? - How can I connect to googlemail.com using TLS and System.Net.Mail.SMTPCLient? 使用System.Net.Mail.SmtpClient,更改SMTP FROM电子邮件地址 - Using System.Net.Mail.SmtpClient, change the SMTP FROM email address 无法使用 System.Net.Mail.SmtpClient 将 email 发送到其他域 - Unable to send email to other domains using System.Net.Mail.SmtpClient 使用TLS和SmtpAuth在.Net 4 / C#中发送带有System.Net.Mail.SmtpClient的电子邮件,可在本地使用,但不能在生产服务器上运行(win 2008 r2) - Sending email with System.Net.Mail.SmtpClient in .Net 4/C# with TLS and SmtpAuth, works locally but not on production server(win 2008 r2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM