Soji 1 Posted July 9, 2021 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
Remy Lebeau 1436 Posted July 9, 2021 (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 July 9, 2021 by Remy Lebeau Share this post Link to post
Soji 1 Posted July 10, 2021 No. The proxy doesn't require it's own authentication. Share this post Link to post
Remy Lebeau 1436 Posted July 10, 2021 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
Soji 1 Posted September 14, 2021 @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
Remy Lebeau 1436 Posted September 14, 2021 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
Soji 1 Posted September 21, 2021 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: But I get the "Connection closed Gracefully" exception when I call IdIMAP.Connect(false). Share this post Link to post
Remy Lebeau 1436 Posted September 21, 2021 (edited) <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 September 21, 2021 by Remy Lebeau 2 1 Share this post Link to post
Joseph MItzen 252 Posted September 22, 2021 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. Share this post Link to post
Remy Lebeau 1436 Posted September 22, 2021 9 hours ago, Joseph MItzen said: Remy, I think you hit your head and have started speaking Java. ? Share this post Link to post
Soji 1 Posted September 23, 2021 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