Recent

Author Topic: Help Needed With Indy SMTP While Sending Email  (Read 3098 times)

AaronCatolico1

  • New Member
  • *
  • Posts: 31
Help Needed With Indy SMTP While Sending Email
« on: November 20, 2024, 01:51:22 am »
I'm not sure what all I need to send through a Socks5 proxy that requires login credentials. What all do I need to include in the "Uses" clause, as well as any components to drag n drop/add to the form.

Here's what I have so far for sending email:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   IdSMTP, IdMessage, IdSSL, IdSSLOpenSSL, IdExplicitTLSClientServerBase, IdText,
  10.   IdAttachmentFile, IdMessageParts;
  11.  
  12. type
  13.  
  14.   {TForm1}
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     IdMessage1: TIdMessage;
  18.     IdSMTP1: TIdSMTP;
  19.     IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
  20.     procedure Button1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. var
  38.   SMTP: TIdSMTP;
  39.   Email: TIdMessage;
  40.   SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
  41.   i:integer;
  42.   ct:string;
  43. begin
  44.   SMTP := TIdSMTP.Create(nil);
  45.   Email := TIdMessage.Create(nil);
  46.   SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  47.   try
  48.     {Set up the SMTP client}
  49.     SMTP.Host := 'smtp.gmail.com';
  50.     SMTP.Port := 587;
  51.     SMTP.Username := 'MyGmailAccount@gmail.com';
  52.     SMTP.Password := 'MyGmailPassword';
  53.     SMTP.IOHandler := SSLHandler;
  54.     SMTP.UseTLS := utUseExplicitTLS;  // Use TLS encryption
  55.  
  56.     {Configure SSL/TLS settings}
  57.     SSLHandler.SSLOptions.Method := sslvTLSv1_2; // TLS version
  58.     SSLHandler.SSLOptions.Mode := sslmClient;
  59.  
  60.     {Compose the email}
  61.     Email.From.Address := 'yourusername@example.com';  // From email address
  62.     Email.Recipients.EmailAddresses := 'recipient1@gmail.com'; // Recipient's email
  63.     Email.Subject := 'My cool subject line';
  64.  
  65.  
  66.     {Specify if it's plain text or html email}
  67.      Email.ContentType := 'multipart/related; type="text/html"';  // Set content type to HTML
  68.      ct := 'text/html';
  69.  
  70.  
  71.  
  72.     {Set the HTML message body}
  73.     email.Messageparts.Clear;
  74.     with TIdText.Create(Email.MessageParts, nil) do begin
  75.       Body.Text := memo1.lines.text;
  76.       ContentType := ct;
  77.     end;
  78.  
  79.  
  80.  
  81.     {Connect to SMTP server and send email}
  82.     SMTP.Connect;
  83.     try
  84.       SMTP.Send(Email);
  85.     finally
  86.       SMTP.Disconnect;
  87.     end;
  88.  
  89.     showmessage('Email sent successfully.');
  90.  
  91.   except
  92.     on E: Exception do
  93.       showmessage('Error sending email: ' + E.Message);
  94.   end;
  95.  
  96.   {Clean up}
  97.   SMTP.Free;
  98.   Email.Free;
  99.   SSLHandler.Free;
  100.  
  101. end;
  102.  
  103.  
  104.  



Anyone have an example I can test out to applying the private proxy credentials to this code?





Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1590
    • Lebeau Software
Re: Help Needed With Indy SMTP While Sending Email
« Reply #1 on: November 20, 2024, 03:28:11 am »
First, why are you dropping components on your Form, but then creating components dynamically in code? Pick one way or the other.

Also, have a look at the TIdMessageBuilderHtml class in the TIdMessageBuilderHtml unit for creating an HTML email.

That being said...

For SOCKS, Indy has a TIdSocksInfo component in the IdSocks unit. Assign it to your IOHandler's TransparentProxy property, and set its properties as needed. For example:

Code: [Select]
uses
  ..., IdSMTP, IdSSLOpenSSL, IdSocks, ...;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  SMTP: TIdSMTP;
  SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
  Socks: TIdSocksInfo;
  ...
begin
  SMTP := TIdSMTP.Create(nil);
  try
    ...
    SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
    SMTP.IOHandler := SSLHandler;
    ...

    Socks := TIdSocksInfo.Create(SSLHandler);
    Socks.Version := svSocks5;
    Socks.Authentication := saUsernamePassword;
    Socks.Host := ... ;
    Socks.Port := ... ;
    Socks.Username := ...;
    Socks.Password := ...;
    SSLHandler.TransparentProxy := Socks;

    ...

  finally
    SMTP.Free;
  end;
 
end;
« Last Edit: November 20, 2024, 03:47:12 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

AaronCatolico1

  • New Member
  • *
  • Posts: 31
Re: Help Needed With Indy SMTP While Sending Email
« Reply #2 on: November 20, 2024, 05:19:24 pm »
Hi Remy! Yeah, so, I tried what you've recommended. I dragged and dropped the 'IdSocksInfo1' on the form and added the additional Socks settings, but I'm not quite sure why I'm still not able to connect through the socks proxy. I'm not sure if the socks5 is any good or not or if I have the wrong configuration in code.
I've attached the two error messages I'm getting to this post. Also, here's the code that I've tried to use below:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   IdSMTP, IdMessage, IdSSL, IdSSLOpenSSL, IdExplicitTLSClientServerBase, IdText,
  10.   IdAttachmentFile, IdMessageParts, IdSocks;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     IdMessage1: TIdMessage;
  19.     IdSMTP1: TIdSMTP;
  20.     IdSocksInfo1: TIdSocksInfo;
  21.     IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
  22.     procedure Button1Click(Sender: TObject);
  23.   private
  24.  
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. var
  40.   SMTP: TIdSMTP;
  41.   Email: TIdMessage;
  42.   SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
  43.   i:integer;
  44.   ct:string;
  45.   Socks: TIdSocksInfo;
  46.  
  47. begin
  48.   SMTP := TIdSMTP.Create(nil);
  49.   Email := TIdMessage.Create(nil);
  50.  
  51.   try
  52.  
  53.     SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
  54.  
  55.     {Set up the SMTP client}
  56.     SMTP.Host := 'smtp.gmail.com';
  57.     SMTP.Port := 587;
  58.     SMTP.Username := 'mygmailaccount1@gmail.com';
  59.     SMTP.Password := 'MY GMAIL PASSWORD';
  60.     SMTP.IOHandler := SSLHandler;
  61.     SMTP.UseTLS := utUseExplicitTLS;  // Use TLS encryption
  62.  
  63.     {Configure SSL/TLS settings}
  64.     SSLHandler.SSLOptions.Method := sslvTLSv1_2; // TLS version
  65.     SSLHandler.SSLOptions.Mode := sslmClient;
  66.  
  67.     {Socks Proxy Settings}
  68.     Socks := TIdSocksInfo.Create(SSLHandler);
  69.     Socks.Version := svSocks5;
  70.     Socks.Authentication := saUsernamePassword;
  71.     Socks.Host := '207.244.217.165';
  72.     Socks.Port := 6712;
  73.     Socks.Username := 'MY SOCKS UN';
  74.     Socks.Password := 'MY SOCKS PW';
  75.     SSLHandler.TransparentProxy := Socks;
  76.  
  77.     {Compose the email}
  78.     Email.From.Address := 'yourusername@example.com';  // From email address
  79.     Email.Recipients.EmailAddresses := 'MYgmailAccount1@gmail.com'; // Recipient's email
  80.     Email.Subject := 'Test with proxy!';
  81.  
  82.  
  83.     {Specify if it's plain text or html email}
  84.  
  85.      Email.ContentType := 'multipart/related; type="text/html"';  // Set content type to HTML
  86.      ct := 'text/html';
  87.  
  88.  
  89.  
  90.     {Set the HTML message body}
  91.     email.Messageparts.Clear;
  92.     with TIdText.Create(Email.MessageParts, nil) do begin
  93.       Body.Text := '<h1>Wut up!</h1>';
  94.       ContentType := ct;
  95.     end;
  96.  
  97.  
  98.  
  99.  
  100.     {Connect to SMTP server and send email}
  101.     SMTP.Connect;
  102.     try
  103.       SMTP.Send(Email);
  104.     finally
  105.       SMTP.Disconnect;
  106.       SMTP.Free;
  107.     end;
  108.  
  109.     showmessage('Email sent successfully.');
  110.  
  111.   except
  112.     on E: Exception do
  113.       showmessage('Error sending email: ' + E.Message);
  114.   end;
  115.  
  116.   {Clean up}
  117.   SMTP.Free;
  118.   Email.Free;
  119.  SSLHandler.Free;
  120.  
  121. end;
  122.  
  123. end.
  124.  
  125.  





First, why are you dropping components on your Form, but then creating components dynamically in code? Pick one way or the other.

Also, have a look at the TIdMessageBuilderHtml class in the TIdMessageBuilderHtml unit for creating an HTML email.

That being said...

For SOCKS, Indy has a TIdSocksInfo component in the IdSocks unit. Assign it to your IOHandler's TransparentProxy property, and set its properties as needed. For example:

Code: [Select]
uses
  ..., IdSMTP, IdSSLOpenSSL, IdSocks, ...;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  SMTP: TIdSMTP;
  SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
  Socks: TIdSocksInfo;
  ...
begin
  SMTP := TIdSMTP.Create(nil);
  try
    ...
    SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
    SMTP.IOHandler := SSLHandler;
    ...

    Socks := TIdSocksInfo.Create(SSLHandler);
    Socks.Version := svSocks5;
    Socks.Authentication := saUsernamePassword;
    Socks.Host := ... ;
    Socks.Port := ... ;
    Socks.Username := ...;
    Socks.Password := ...;
    SSLHandler.TransparentProxy := Socks;

    ...

  finally
    SMTP.Free;
  end;
 
end;

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1590
    • Lebeau Software
Re: Help Needed With Indy SMTP While Sending Email
« Reply #3 on: November 20, 2024, 05:45:15 pm »
I dragged and dropped the 'IdSocksInfo1' on the form

Again, I ask - WHY are you dropping components onto your Form but then not using them in your code?  You are just wasting memory and resources that way.

I'm not quite sure why I'm still not able to connect through the socks proxy. I'm not sure if the socks5 is any good or not or if I have the wrong configuration in code.
I've attached the two error messages I'm getting to this post.

The error you are getting is coming from the SOCKS proxy itself, which means you are connected to it and communicating with it, you just don't seem to have permission to create a tunnel through it.  It is accepting your credential login but then denying the tunnel request.  Double-check the proxy's configuration is correct for your login.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

AaronCatolico1

  • New Member
  • *
  • Posts: 31
Re: Help Needed With Indy SMTP While Sending Email
« Reply #4 on: November 20, 2024, 05:55:27 pm »
What I'm saying is that I drag and drop the components such as TIdSMTP, TIdMessage, TIdSSLIOHandlerSocketOpenSSL, IdSocksInfo on the form, then access the components as shown in my code. I do it this way because when I don't drag & drop them first, then all I receive are error messages indicating that those components are missing. It only seems to work if I first drag and drop them on the form. Most tutorials I've seen on YouTube, they always drag and drop them first which is why I'm doing it this way. Is there some other way of doing it without having to drag and drop them on the form as a prerequisite? I tried it without dragging first, but it throws erros and doesn't allow me to proceed without doing so.

BTW, I'm using the FREE private proxies from webshare.io in which they give you a ton of FREE proxies to test out that require login credentials. I'm able to navigate in Google Chrome behind their private proxies, but not sure why I wouldn't be able to tunnel in through email. I will double check with them to see what they have to say about it. In the meantime, do you know of any socks5 proxies I could test out or some that you've personally tried or would recommend? Thanks again, Remy.


I dragged and dropped the 'IdSocksInfo1' on the form

Again, I ask - WHY are you dropping components onto your Form but then not using them in your code?  You are just wasting memory and resources that way.

I'm not quite sure why I'm still not able to connect through the socks proxy. I'm not sure if the socks5 is any good or not or if I have the wrong configuration in code.
I've attached the two error messages I'm getting to this post.

The error you are getting is coming from the SOCKS proxy itself, which means you are connected to it and communicating with it, you just don't seem to have permission to create a tunnel through it.  It is accepting your credential login but then denying the tunnel request.  Double-check the proxy's configuration is correct for your login.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1590
    • Lebeau Software
Re: Help Needed With Indy SMTP While Sending Email
« Reply #5 on: November 21, 2024, 01:44:33 am »
What I'm saying is that I drag and drop the components such as TIdSMTP, TIdMessage, TIdSSLIOHandlerSocketOpenSSL, IdSocksInfo on the form, then access the components as shown in my code.

No, you are dropping the components onto your Form, and then IGNORING THEM, instead creating SEPARATE component instances dynamically in the code.  So, you are DUPLICATING the components in memory, and then using only HALF of them.

You should EITHER drop the components onto the Form and actually use them, or else DON'T drop them at all and just create them dynamically in code.  DON'T DO BOTH.

I do it this way because when I don't drag & drop them first, then all I receive are error messages indicating that those components are missing.

If you have the relevant unit references in the uses clause, and are linking to the Indy packages, then you'll be fine.

It only seems to work if I first drag and drop them on the form. Most tutorials I've seen on YouTube, they always drag and drop them first which is why I'm doing it this way.

Such tutorials are liking USING the components that they drop.  There is no point in dropping a component and then ignoring it.

Is there some other way of doing it without having to drag and drop them on the form as a prerequisite? I tried it without dragging first, but it throws erros and doesn't allow me to proceed without doing so.

What you could do is first drop the components onto the Form just to establish the references, and then delete the components from the Form, and then create them dynamically.  Or, you could just stop creating them dynamically and use the ones that you dropped on the Form.

BTW, I'm using the FREE private proxies from webshare.io in which they give you a ton of FREE proxies to test out that require login credentials. I'm able to navigate in Google Chrome behind their private proxies

SOCKS is a standardized protocol.  I suggest you use a packet sniffer like Wireshark to compare Chrome's SOCKS traffic to Indy's SOCKS traffic, maybe you will see a difference (ie, maybe Chrome is using a different authentication than Indy, etc).

but not sure why I wouldn't be able to tunnel in through email.

This has nothing to do with email.  At the point where the tunnel is being created, the proxy doesn't know (or care) how the tunnel will be used.  The client is merely asking the proxy to tunnel to a given IP/Port, nothing more.  You can replace TIdSMTP with TIdTCPClient and you should get the same error.

do you know of any socks5 proxies I could test out or some that you've personally tried or would recommend?

No.
« Last Edit: November 21, 2024, 05:28:18 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

rvk

  • Hero Member
  • *****
  • Posts: 7017
Re: Help Needed With Indy SMTP While Sending Email
« Reply #6 on: November 21, 2024, 09:46:32 am »
BTW. Even if you get everything up and running you will run into this:

Code: Pascal  [Select][+][-]
  1. SMTP.Password := 'MY GMAIL PASSWORD';

Did you really use your GMail password there? Google doesn't allow that anymore.
You need to create an App password for sending mail.
Or you need to go through the OAuth2 process. But App password is the easiest and still works.
But your own GMail password isn't going to work anymore.

AaronCatolico1

  • New Member
  • *
  • Posts: 31
Re: Help Needed With Indy SMTP While Sending Email
« Reply #7 on: November 21, 2024, 05:45:28 pm »
lol No, I'm already aware of this. Even if you tried, I don't think Gmail allows the connection with your real password. You have to go into your gmail settings to allow smtp access with a temporary password.

BTW. Even if you get everything up and running you will run into this:

Code: Pascal  [Select][+][-]
  1. SMTP.Password := 'MY GMAIL PASSWORD';

Did you really use your GMail password there? Google doesn't allow that anymore.
You need to create an App password for sending mail.
Or you need to go through the OAuth2 process. But App password is the easiest and still works.
But your own GMail password isn't going to work anymore.

AaronCatolico1

  • New Member
  • *
  • Posts: 31
Re: Help Needed With Indy SMTP While Sending Email
« Reply #8 on: November 21, 2024, 05:48:53 pm »
Yeah Remy, There was nothing wrong with the current configurations I have. I've discovered that all of my settings were correct. You were right about this issue being with the Socks5 private proxy. They don't allow for sending email through their proxy servers. Understandable. Thank you for your clarification.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1590
    • Lebeau Software
Re: Help Needed With Indy SMTP While Sending Email
« Reply #9 on: November 22, 2024, 05:22:06 pm »
They don't allow for sending email through their proxy servers.

Then they must be filtering on the specific host/port that you are tunneling to after you have logged in.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018