Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理401未经授权使用Exchange EWS发送电子邮件_C#_Asp.net_Asp.net Mvc 4_Exchangewebservices - Fatal编程技术网

C# 处理401未经授权使用Exchange EWS发送电子邮件

C# 处理401未经授权使用Exchange EWS发送电子邮件,c#,asp.net,asp.net-mvc-4,exchangewebservices,C#,Asp.net,Asp.net Mvc 4,Exchangewebservices,我有以下使用ExchangeEWS发送电子邮件的代码。在提供错误的用户名和密码并返回401未经授权的错误之前,一切正常。我将发送包装在catch语句中以处理错误。但是没有达到catch语句 public void SendExchangeEmail(EmailModel model, ApplicationUser adminUser) { var service = new ExchangeService(ExchangeVersion.Exchange2013_SP1) {

我有以下使用ExchangeEWS发送电子邮件的代码。在提供错误的用户名和密码并返回401未经授权的错误之前,一切正常。我将发送包装在catch语句中以处理错误。但是没有达到catch语句

public void SendExchangeEmail(EmailModel model, ApplicationUser adminUser) 
{
    var service = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
    {
        Credentials = new WebCredentials(adminUser.Email, adminUser.ExchangePassword),
        TraceEnabled = true,
        TraceFlags = TraceFlags.All,
        Url = new Uri("MyExchangeUrl")
    };

    var email = new EmailMessage(service);
    email.ToRecipients.Add(model.recipient);
    email.Subject = model.Subject;
    email.Body = new MessageBody(model.Body);

    try
    {
        email.Send();
    }
    catch (ServiceResponseException ex)
    {
         // This catch block is not reached when the incorrect username and password are supplied. 
    }
}

捕获未授权错误的正确方法是什么

您需要与Exchange管理员联系,以允许服务帐户(AD帐户)在outlook应用程序之外发送电子邮件(我忘记了角色/权限的特定名称)

然后修改代码以使用以下内容:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1); service.Credentials = new System.Net.NetworkCredential(serviceAccount.UserName, serviceAccount.Password); service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, userEmail); service.AutodiscoverUrl(userEmail, RedirectionUrlValidationCallback); ExchangeService服务=新的ExchangeService(ExchangeVersion.Exchange2013\u SP1); service.Credentials=新系统.Net.NetworkCredential(servicecomport.UserName,servicecomport.Password); service.ImpersonatedUserId=新的ImpersonatedUserId(ConnectingIdType.SmtpAddress,userEmail); 自动发现URL(用户电子邮件、重定向URL验证回调);
电子邮件用户的权限仅在outlook应用程序中有效。如果在outlook应用程序外部调用该命令,则该命令将被阻止,因此需要Exchange管理员知道的权限。

您需要与Exchange管理员联系,以允许服务帐户(AD帐户)获得在outlook应用程序外部发送电子邮件的权限(我忘记了角色/权限的特定名称)

然后修改代码以使用以下内容:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1); service.Credentials = new System.Net.NetworkCredential(serviceAccount.UserName, serviceAccount.Password); service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, userEmail); service.AutodiscoverUrl(userEmail, RedirectionUrlValidationCallback); ExchangeService服务=新的ExchangeService(ExchangeVersion.Exchange2013\u SP1); service.Credentials=新系统.Net.NetworkCredential(servicecomport.UserName,servicecomport.Password); service.ImpersonatedUserId=新的ImpersonatedUserId(ConnectingIdType.SmtpAddress,userEmail); 自动发现URL(用户电子邮件、重定向URL验证回调);
电子邮件用户的权限仅在outlook应用程序中有效。如果命令在外部调用,它将被阻止,因此需要Exchange管理员知道的权限。

您的异常处理不正确。“请求失败。远程服务器返回错误:(401)未经授权。”错误引发ServiceRequestException。将代码修改为:

try
{
    email.Send();
}
catch (ServiceRequestException ex)
{
    //Exception handling
}

您的异常处理不正确。“请求失败。远程服务器返回错误:(401)未经授权。”错误引发ServiceRequestException。将代码修改为:

try
{
    email.Send();
}
catch (ServiceRequestException ex)
{
    //Exception handling
}

这很有效。我知道我会错过一些简单的事情。谢谢,希普这工作做得很好。我知道我会错过一些简单的事情。谢谢你