在 Azure Web 或辅助角色中使用 SmtpClient 时出现异常。
我创建了一个控制台应用程序,通过 RDP 在角色虚拟机上手动运行以进行重现:
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("[email protected] /cdn-cgi/l/email-protection", "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("[email protected] /cdn-cgi/l/email-protection");
message.To.Add(new MailAddress("[email protected] /cdn-cgi/l/email-protection"));
message.Subject = "testing " + DateTime.UtcNow;
message.Body = "The quick brown fox jumped over the lazy dogs.";
mailClient.Send(message);
}
}
}
在本地它发送电子邮件就很好。在 Azure 上我得到这个:
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。