Jump to content
Soji

How to connect to office 365 using proxy server

Recommended Posts

Hi,
I am trying to figure out how to connect to office 365 through a proxy server. My application is working well if I connect without a proxy. But now the requirement changed and direct connection to internet is not allowed and I have to use a proxy. I can connect to office 365 from the production machine through proxy using a browser. So the connection setup is good. Now I need to change my application to use a proxy. So I looked around and found that I have to use TIdIOHandlerStack and IdConnectThroughHttpProxy1 to achieve it. But the help document is not that great. So looking for suggestions/help.

 

My code looks like this.

type
  TForm1 = class(TForm)
    Button1: TButton;
    IdIMAP: TIdIMAP4;
    IdConnectThroughHttpProxy1: TIdConnectThroughHttpProxy;
    IdIOHandlerStack1: TIdIOHandlerStack;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdIMAP.Username := 'test@test.com'; // outlook user name
  IdIMAP.Password := 'xxxxx'; // outlook user password
  IdIMAP.Port := 993; // outlook server port
  IdIMAP.Host := 'outlook.office365.com'; // outlook server
  IdIMAP.AuthType := iatUserPass;

  IdConnectThroughHttpProxy1.Host := 'x.x.x.x'; // Proxy server
  IdConnectThroughHttpProxy1.Port := 1234; // Proxy server port

  IdIOHandlerStack1.TransparentProxy := IdConnectThroughHttpProxy1;

  IdIMAP.IOHandler := IdIOHandlerStack1;

  try
    Memo1.Lines.Clear;
    Memo1.Lines.Add('Trying to connect to server...');
    if not IdIMAP.Connected then
    begin
      if IdIMAP.Connect(false) then
        Memo1.Lines.Add('Connected to server')
      else
        Memo1.Lines.Add('NOT Connected to server')
    end;

    Memo1.Lines.Add('Trying to login to server...');
    if not(IdIMAP.ConnectionState in [csAuthenticated, csSelected]) then
      IdIMAP.Login;
    Memo1.Lines.Add('Logged in to server');
  except
    on E: Exception do
    begin
      Memo1.Lines.Add('Error while connecting: ' + E.Message);
    end;
  end;
end;

When I try this, I get "Error while connecting: 403 forbidden error"

 

Thanks for your time,

Kind regards,

Soji.

Share this post


Link to post
Posted (edited)
2 hours ago, Soji said:

When I try this, I get "Error while connecting: 403 forbidden error"

Does the proxy in question require its own authentication?  If so, does it support BASIC authentication, or does it require a different authentication?

 

Edited by Remy Lebeau

Share this post


Link to post

No. The proxy doesn't require it's own authentication. 

Share this post


Link to post

Does the proxy require the client to use HTTPS rather than HTTP?  TIdConnectThroughHttpProxy has no concept of HTTPS.  If the proxy requires HTTPS, you could try using a TIdSSLIOHandlerSocketBase-derived component, such as TIdSSLIOHandlerSocketOpenSSL, instead of TIdIOHandlerStack, and set the IOHandler's PassThrough property to false before connecting.  Though, I think that will also affect TIdIMAP4, too.

 

Does the 403 error contain any content on the wire (TIdConnectThroughHttpProxy does not expose access to this) to explain why the connection is being rejected?

Share this post


Link to post

@Remy Lebeau: Thanks for your suggestion. I checked it but we use HTTP. So I used TIdConnectThroughHttpProxy and I investigated a bit more and adjusted code like this:
 

  p := TIdConnectThroughHttpProxy.Create(IdIMAP);
  io := TIdIOHandlerStack.Create(IdIMAP);
  p.Host :='x.x.x.x'; // Proxy server ip
  p.port :=1234; // Proxy server port
  p.Enabled := true;
  io.TransparentProxy:=p;

  IdIMAP.IOHandler := io;
  IdIMAP.Host := 'outlook.office365.com';
  IdIMap.Port := 993;
  IdIMAP.Password := 'Pass123';

Now I get the "Connection closed Gracefully" exception when I call IdIMAP.Connect(false). 
Thanks for your time,
Soji.

Share this post


Link to post
1 hour ago, Soji said:

@Remy Lebeau: Thanks for your suggestion. I checked it but we use HTTP. So I used TIdConnectThroughHttpProxy and I investigated a bit more and adjusted code like this:
 

Now I get the "Connection closed Gracefully" exception when I call IdIMAP.Connect(false).

Without seeing a trace log of what's going on over the wire, I honestly could not tell you what is going on.  Can you get a Wireshark capture of the connect attempt?

Share this post


Link to post
On 9/14/2021 at 6:03 PM, Remy Lebeau said:

Without seeing a trace log of what's going on over the wire, I honestly could not tell you what is going on.  Can you get a Wireshark capture of the connect attempt?

Thanks @Remy Lebeau for your time.

I received a network log from the production server. They said that it is actually connecting to outlook through proxy! See the image below:

image.thumb.png.022888b1e632de712778ee6d5af42c02.png

 

But I get the "Connection closed Gracefully" exception when I call IdIMAP.Connect(false). 

Share this post


Link to post

<ARG!> I just now noticed that you are connecting to Office 365 on port 993.  That is an IMPLICIT TLS port.  But, you are not using any TLS settings on your TIdIMAP4 at all!  Rather than assigning a TIdIOHandlerStack component to the TIdIMAP4.IOHandler property, you need to assign a TIdSSLIOHandlerSocketBase-derived component instead, like TIdSSLIOHandlerSocketOpenSSL (TIdSSLIOHandlerSocketBase derives from TIdIOHandlerStack and thus also has the TransparentProxy property).  And then set the TIdIMAP4.UseTLS property to utUseImplicitTLS.

Edited by Remy Lebeau
  • Like 2
  • Thanks 1

Share this post


Link to post
12 hours ago, Remy Lebeau said:

 Rather than assigning a TIdIOHandlerStack component to the TIdIMAP4.IOHandler property, you need to assign a TIdSSLIOHandlerSocketBase-derived component instead, like TIdSSLIOHandlerSocketOpenSSL (TIdSSLIOHandlerSocketBase derives from TIdIOHandlerStack and thus also has the TransparentProxy property).  And then set the TIdIMAP4.UseTLS property to utUseImplicitTLS.

Remy, I think you hit your head and have started speaking Java. :classic_tongue:

Share this post


Link to post
9 hours ago, Joseph MItzen said:

Remy, I think you hit your head and have started speaking Java. :classic_tongue:

?

Share this post


Link to post
On 9/21/2021 at 8:20 PM, Remy Lebeau said:

<ARG!> I just now noticed that you are connecting to Office 365 on port 993.  That is an IMPLICIT TLS port.  But, you are not using any TLS settings on your TIdIMAP4 at all!  Rather than assigning a TIdIOHandlerStack component to the TIdIMAP4.IOHandler property, you need to assign a TIdSSLIOHandlerSocketBase-derived component instead, like TIdSSLIOHandlerSocketOpenSSL (TIdSSLIOHandlerSocketBase derives from TIdIOHandlerStack and thus also has the TransparentProxy property).  And then set the TIdIMAP4.UseTLS property to utUseImplicitTLS.

@Remy Lebeau: Thanks that worked like a charm... You were really helpful. Thanks for taking time to help me with this.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×