"不支持请求的功能"在Azure角色使用SmtpClient时异常 [英] "The function requested is not supported" exception when using SmtpClient in Azure role

查看:237
本文介绍了"不支持请求的功能"在Azure角色使用SmtpClient时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Azure中的Web或辅助角色使用SmtpClient时,得到一个例外。

I'm getting an exception when using SmtpClient in an Azure Web or Worker role.

我创建了一个控制台应用程序对通过RDP角色的虚拟机手动运行复制:

I created a console app to manually run on the role VMs via RDP to reproduce:

using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace ConsoleApplication1
{
   class Program
   {
      static void Main()
      {
         var mailClient = new SmtpClient("mail.redacted.com", 587);
         mailClient.EnableSsl = true;
         mailClient.DeliveryFormat = SmtpDeliveryFormat.International;
         mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

         mailClient.UseDefaultCredentials = false;//SET THIS FIRST OR IT WIPES OUT CREDENTIALS
         NetworkCredential netCreds = new NetworkCredential("mail@redacted.com", "12345 same combination on my luggage");
         mailClient.Credentials = netCreds;

         MailMessage message = new MailMessage();
         message.SubjectEncoding = Encoding.UTF8;
         message.BodyEncoding = Encoding.UTF8;
         message.IsBodyHtml = false;

         message.From = new MailAddress("mike@redacted.com");
         message.To.Add(new MailAddress("mike@redacted.com"));

         message.Subject = "testing " + DateTime.UtcNow;
         message.Body = "The quick brown fox jumped over the lazy dogs.";

         mailClient.Send(message);
      }
   }
}

本地发送一封电子邮件,就好了。 Azure上我得到这样的:

Locally it sends an email just fine. On Azure I get this:


    Unhandled Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.ComponentModel.Win32Exception: The function requested is not supported
       at System.Net.NTAuthentication.GetOutgoingBlob(Byte[] incomingBlob, Boolean throwOnError, SecurityStatus& statusCode)
       at System.Net.NTAuthentication.GetOutgoingBlob(String incomingBlob)
       at System.Net.Mail.SmtpNtlmAuthenticationModule.Authenticate(String challenge, NetworkCredential credential, Object sessionCookie, String spn, ChannelBinding channelBindingToken)
       at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       --- End of inner exception stack trace ---
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at ConsoleApplication1.Program.Main() in c:\development\ConsoleApplication1\ConsoleApplication1\Program.cs:line 39

我已经证实了Azure的机器可以通过RDP在Azure的角色运行TCPing.exe接入端口587在邮件服务器上。

I've confirmed that the Azure machines can access port 587 on the mail server by running TCPing.exe on the Azure roles via RDP.

推荐答案

因此​​很明显,这个问题是服务器之间不匹配的NTLM版本。

So apparently the problem was a mismatched NTLM version between servers.

在登录到Azure的角色和禁止设置要求NTLMv2安全客户端,那么它的工作:

After logging into the Azure roles and disabling the setting "Require NTLMv2 security" for clients, then it worked:

(感谢这个答案和的这个答案获得灵感。)

(Thanks to this answer and this answer for inspiration.)

目前看,如果我们能够得到我们的SMTP服务器升级为NTLMv2的兼容。否则,我们将不得不建立一些自动化的code以某种方式禁用每个产生的角色实例的设置。

Currently seeing if we can get our SMTP server upgraded to be NTLMv2-compatible. Otherwise we'll have to set up some automated code to somehow disable that setting on each generated role instance.

显然,这code工作的最后一个月。所以我猜最近的Azure操作系统升级更改默认设置。

Apparently this code worked last month. So I'm guessing a recent Azure OS upgrade changed the default settings.

FYI:注册表项为此设置为

FYI: The registry key for this setting is

[HKEY_LOCAL_MACHINE \系统\ CurrentControlSet \控制\ LSA \ MSV1_0]   NtlmMinClientSec= DWORD:20000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0] "NtlmMinClientSec"=dword:20000000

要自动设置注册表项添加启动任务包含 REG ADD 命令是这样的:

To automate setting the registry key add a startup task containing a reg add command like this:

reg add HKLM\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 ^
 /v NtlmMinClientSec ^
 /t REG_DWORD ^
 /d 0x20000000 ^
 /f

其中, /˚F强制当前设置被覆盖, ^ 只允许打破命令成多​​行为更好的可读性。另外,还要确保以ASCII编码保存命令prevent <一href="http://stackoverflow.com/questions/15875165/error-running-powershell-commands-on-azure-workerrole">issues在角色启动。

where /f forces the current setting to be overwritten and ^ just allows to break the command into multiple lines for better readability. Also make sure to save the command in ASCII encoding to prevent issues during role startup.

这篇关于&QUOT;不支持请求的功能&QUOT;在Azure角色使用SmtpClient时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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