EWS code return Error : Request failed. The remote server returned an error: (403) Forbidden  OR  (401) Unauthorized



I am quite unhappy with the limited documentation on EWS.  There should be many useful examples showing the power of EWS and not
just how to call a function.  Also when technology is new , there should be many Videos/Writeup on troubleshooting with EWS with Steps and snapshots.


Now I am getting the following Error message and i could not find any resource which can direct me to a solution. 

Microsoft.Exchange.WebServices.Data.ServiceRequestException was unhandled
  Message="Request failed. The remote server returned an error: (403) Forbidden."
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.InternalExecute()
       at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
       at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalFindFolders(IEnumerable`1 parentFolderIds, SearchFilter searchFilter, FolderView view, ServiceErrorHandling errorHandlingMode)
       at Microsoft.Exchange.WebServices.Data.ExchangeService.FindFolders(FolderId parentFolderId, FolderView view)
       at Microsoft.Exchange.WebServices.Data.ExchangeService.FindFolders(WellKnownFolderName parentFolderName, FolderView view)
       at EWSTest1.Form1.button1_Click(Object sender, EventArgs e) in c:\temp\EWS\EWSTest1\EWSTest1\Form1.cs:line 41
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at EWSTest1.Program.Main() in c:\temp\EWS\EWSTest1\EWSTest1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Net.WebException
       Message="The remote server returned an error: (403) Forbidden."
            at System.Net.HttpWebRequest.GetResponse()
            at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.Emit()
            at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.InternalExecute()


Following is my code.

 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

   service.TraceEnabled = true;

   service.Credentials = new WebCredentials("xx@xx.com", "xxxx123");

   service.Url = new Uri("https://mail.xxxxxx.com/EWS/Exchange.asmx");

   FolderView v = new FolderView(5);

   ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;

   service.FindFolders(WellKnownFolderName.Inbox, v);

   //Error on above line.

 I tried changing the code line as below and I get another Error ....

service.Credentials = new NetworkCredential("xx@xx.com", "xx","xxxxxxxx");
"The remote server returned an error: (401) Unauthorized."


How do i resolve.   and what is difference between NetworkCredential and WebCredentials ?

July 12th, 2010 7:35pm

Hello, assuming login and password are correct it may be a problem of the type of the authentication. For example NTLM authentication cannot be delegated. Look here


Free Windows Admin Tool Kit Click here and download it now
July 13th, 2010 1:26pm

Hi Jirka,

I don;t understand the link well, I feel its not related to the problem i am facing, as i am not using sharepoint or anything else. 

Mine is a very simple setup.  One ExchangeServer and One XP laptop trying to execute the code.


July 13th, 2010 1:48pm

Maybe there is not enabled SSL on the target machine. Try this  service.Url = new Uri("http://mail.xxxxxx.com/EWS/Exchange.asmx"); (http instead https).
Free Windows Admin Tool Kit Click here and download it now
July 13th, 2010 4:07pm


SSL is enabled.   Any other pointers ?



July 13th, 2010 7:18pm

403 usually means that SSL is required, but since you are already using https it is possibly something else.  There are actually 20 different types of 403 response - I think it would help if you could find the relevant entry in your iis log files on the target server, and see what the substatus is.
Free Windows Admin Tool Kit Click here and download it now
July 14th, 2010 5:52pm

This is Resolved.

I figure out that the problem was in code line

service.Credentials = new NetworkCredential("xx@xx.com", "xx","xxxxxxxx");

Instead of xx@xx.com  it should be just username 'xx'.     That Solved.



July 15th, 2010 2:12pm

Hi Jignesh,

Thanks for sharing this.

removing @xx.com worked for me as well.

Thanks again.


Free Windows Admin Tool Kit Click here and download it now
February 5th, 2013 5:05pm


for Java use ExchangeCredentials credentials = new WebCredentials("username", "password"); instead.

June 24th, 2013 6:51pm

Hi Jaywant 
I m not getting the jar contains NetworkCredentials class.

Can you please  help me for same ?

Free Windows Admin Tool Kit Click here and download it now
June 24th, 2013 7:24pm

Hi Jirka_
I m not getting any of the member of service object.

for example credentials member. 

<p>service.Credentials = new NetworkCredential("xx@xx.com", "xx","xxxxxxxx");</p><p>It is saying it is not field.</p><p></p>
June 24th, 2013 8:18pm

Hi Everyone ,

Can any one of you tell me "Can I send mail using java through MS Exchange client."

Please .......

Free Windows Admin Tool Kit Click here and download it now
July 4th, 2013 2:04am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics