SmtpClient向Hosted Exchange Server发送错误消息-试图以访问权限xxx.xxx.xxx.x

本文关键字:xxx 访问 访问权 权限 消息 Exchange Hosted Server 错误 SmtpClient | 更新日期: 2023-09-27 18:13:03

这几个星期我一直在为这个问题绞尽脑汁,但今天是我答应把它修好的日子。到目前为止,我失败了。

我试图从一个托管MVC应用程序发送电子邮件,通过托管Exchange服务器。IT部门已经表示并确认他们已经允许MVC应用程序的IP通过。然而,下面的代码给了我"试图以访问权限禁止的方式访问套接字###.###.###。"###:25" every time.

ActionResult Test()
{
    MailMessage message = new MailMessage();
    message.From = new MailAddress("valid.email.address");
    message.Subject = "Test Email";
    message.To.Add(new MailAddress("another.valid.email.address"));
    message.Body = "Hey, this is a test!";
    using (SmtpClient client = new SmtpClient())
    {
        client.Credentials = new System.Net.NetworkCredential("username", "password");
        client.Port = 25;
        client.EnableSsl = true; // Either true or false gives same result
        client.Host = "actual.host.url";
        try
        {
            client.Send(message);
        }
        catch (Exception ex)
        {
            ViewBag.LogMessage = string.Format("Error: {0}<br />{1}<br />{2}", ex.Message, ex?.InnerException.Message, ex?.InnerException?.InnerException.Message);
            return View();
        }
        ViewBag.LogMessage = string.Format("client.Host: {0}<br />Client.Port: {1}<br />Client.EnableSsl: {2}<br />message.To[0].Address: {3}", client.Host, client.Port, client.EnableSsl ? "true" : "false", message.To[0].Address);
    }
    return View();
}

LogMessage的值为:

Error: Failure sending mail.
Unable to connect to the remote server
An attempt was made to access a socket in a way forbidden by its access permissions ###.###.###.###:25

欢迎提出任何建议!我试过587端口,但没有成功。我已经尝试过有和没有凭据,有或没有SSL,用户名匹配从地址或不。我已经没有什么可尝试的了。

谢谢!

SmtpClient向Hosted Exchange Server发送错误消息-试图以访问权限xxx.xxx.xxx.x

尝试下面的代码,第一行告诉SMTP客户端忽略SSL证书的任何问题,如果它来自无效的提供者。

 ServicePointManager.ServerCertificateValidationCallback =(sender,certificate, chain, sslPolicyErrors) => true;
        var smtpClient = new SmtpClient("oba.exchangeserver.com")
        {
            Port = 587,
            EnableSsl = true,
            Credentials =
                new NetworkCredential("username","password")
        };
        return smtpClient;