How to send email in Delphi?

Member

by carey , in category: Other , a year ago

How to send email in Delphi?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by jimmie.mante , a year ago

@carey 

To send an email in Delphi, you can use the Indy (Internet Direct) library, which is a set of Internet components that are included with Delphi.


Here is an example of how to use Indy to send an email in Delphi:

  1. First, add the Indy components to your Delphi project. To do this, go to the "Component" menu and select "Install Packages". In the "Install Packages" dialog, select "Indy Clients" and click "OK". This will add the Indy components to the "Internet" tab in the component palette.
  2. Drag and drop an "IdSMTP" component onto your form. This component will be used to send the email.
  3. Drag and drop an "IdMessage" component onto your form. This component will be used to create the email message.
  4. Set the properties of the "IdMessage" component to specify the subject, recipient, and body of the email. For example:


IdMessage1.Subject := 'Test Email'; IdMessage1.Recipients.EMailAddresses := '[email protected]'; IdMessage1.Body.Text := 'This is a test email sent from Delphi';

  1. Set the properties of the "IdSMTP" component to specify the SMTP server and login credentials (if necessary). For example:


IdSMTP1.Host := 'smtp.example.com'; IdSMTP1.Username := '[email protected]'; IdSMTP1.Password := 'password';

  1. Finally, call the "Send" method of the "IdSMTP" component to send the email. For example:


IdSMTP1.Connect; try IdSMTP1.Send(IdMessage1); finally IdSMTP1.Disconnect; end;


That's it! You should now have a basic Delphi program that can send an email using the Indy library.

by silas_gulgowski , 4 months ago

@carey 

Here is a complete example code for sending an email in Delphi using Indy:

  1. Add the necessary units to the "uses" clause in your unit:
1
2
uses
  ..., IdSMTP, IdMessage, IdText, IdAttachmentFile;


  1. Create an instance of the TIdSMTP and TIdMessage components:
1
2
3
4
5
6
var
  SMTP: TIdSMTP;
  Msg: TIdMessage;
begin
  SMTP := TIdSMTP.Create(nil);
  Msg := TIdMessage.Create(nil);


  1. Set the properties of the TIdSMTP component to specify the SMTP server and login credentials:
1
2
3
4
5
SMTP.Host := 'smtp.example.com';
SMTP.Username := '[email protected]';
SMTP.Password := 'password';
SMTP.Port := 587; // Or the appropriate port number for your email provider
SMTP.UseTLS := utUseExplicitTLS; // Or utUseImplicitTLS if needed


  1. Set the properties of the TIdMessage component to specify the email details:
1
2
3
4
5
Msg.From.Name := 'Sender Name';
Msg.From.Address := '[email protected]';
Msg.Recipients.Add.Address := '[email protected]';
Msg.Subject := 'Test Email';
Msg.Body.Text := 'This is a test email sent from Delphi';


  1. (Optional) Add attachments to the email:
1
2
3
4
5
var
  Attachment: TIdAttachmentFile;
begin
  Attachment := TIdAttachmentFile.Create(Msg.MessageParts, 'C:path	oattachment.pdf');
end;


  1. Send the email:
1
2
3
4
5
6
try
  SMTP.Connect;
  SMTP.Send(Msg);
finally
  SMTP.Disconnect;
end;


  1. Free the components:
1
2
3
Msg.Free;
SMTP.Free;
end;


Make sure to replace the placeholder values with your actual email server details and email content.