简体   繁体   中英

Sending e-mail through C#, Exchange and EWS Managed API gives error 407: Request failed - Proxy authentication required. Why?

I am writing a Windows forms application based on C# and the EWS Managed API 2.2.0 (Microsoft.Exchange.WebServices.dll assembly) for a client of mine who works on a company.

The app's logic is simple: My client puts his e-mail address "sender@ABC.domain.com" and the recipient's e-mail address "recipient@contoso.com" and the app sends an HTML-based e-mail to the recipient.

The app must connect to the Exchange Server of my client's company, authenticate my client to Exchange as a Windows domain user (using Windows domain authentication) (client is already logged in Windows with his username "sender" and as this Windows user makes the request) and then, send the E-Mail through his mailbox "sender@ABC.domain.com".

I have wrote the above code so far which seems to be correct, but it gives me this error: "The request failed. The remote server returned an error: (407). Proxy Authentication required"

    try{

        //Initialize and bound 'service' to the latest known version of Exchange
        ExchangeService service = new ExchangeService();

        //Use the Windows logged-in user domain account credentials:
        //Is this correct / is this enough???
        service.UseDefaultCredentials = true;

        ServicePointManager.ServerCertificateValidationCallback = myValidationCallBackFunction;

        service.AutodiscoverUrl("sender@ABC.domain.com", myRedirectionCallback);
        MessageBox.Show("Autodiscover Exchange URL found:" + service.Url.ToString());

        //Impersonation test - just in case - but this is not make any difference in the error
        service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "sender@ABC.domain.com");

        //Initialize and set an EmailMessage
        EmailMessage message = new EmailMessage(service);
        message.Subject = "HelloWorld";

        string emailMessageBody = "<!DOCTYPE html><html> ... </html>";

        message.Body = new MessageBody(BodyType.HTML, emailMessageBody);
        email.ToRecipients.Add("recipient@contoso.com");

        message.SendAndSaveCopy();

        MessageBox.Show("E-Mail sent successfully");

    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }

My client uses Windows 10. His PC is part of a Windows domain with url "XYZ.domain.com" and his username on Windows is "sender". He also has an Outlook app installed in his PC which connects perfectly to the Exchange Server on his company's network...

I am totally amateur with Windows domain accounts / authentication. Is this a Windows domain authentication problem? Is this an Exchange conection settings problem? Or the problem is in my code in the authentication section (do I have to add something more)?

I found a part of solution by my own!

Now my code works fine.

The problem was in the Proxy Settings in Windows 10 on my client's PC. For some reason he had Manual proxy setup enabled but he didn't know it. When I found it and disabled it, then the e-mail was sent fine.

I'm saying that I found "a part" of solution because:

1) The Outlook 365 works fine with the on premises Exchange Server 2010, even if I have the proxy settings enabled or not. But my app works only with proxy settings disabled. Does anyone know how this is explained? Why Outlook works fine with both ways?

2) If the IT of the company of my client forces him to have always enabled the proxy settings, how can I bypass this error? Do I have to pass somehow the User's credentials (with or without password)? If, yes, then how can I do this with C#?

  1. I think it works fine both ways because your customer has "Don't use the proxy server for local (intranet) addresses" enabled in Windows 10 Proxy Settings. That would explain why the proxy is bypassed when connecting to the on-premise Exchange Server.
    It doesn't explain however why that doesn't happen when your app connects to the local Exchange Server (?)

  2. You can configure EWS to use a System.Net.WebProxy like this:

WebProxy myProxy = new WebProxy("http://myproxy", 8080)
{
    BypassProxyOnLocal = true,
    Credentials = new NetworkCredentials("username", "password)
};

exchangeService.WebProxy = myProxy;

What credentials you use depends on your client's Proxy infrastructure. If it requires a domain account you can set myProxy.UseDefaultCredentials = true to pass in the current user.

Hth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM