How to send a mail message using Indy's smtp server component?
Asked Answered
F

2

10

Since the demo given in http://www.indyproject.org/Sockets/Demos/index.EN.aspx only saves the received stream to a file, I don't know how to effectevely send that stream as a mail.

Could anyone teach me how to do this or to point me some more complete example?

Figurant answered 14/3, 2010 at 1:58 Comment(0)
B
9

A SMTP server component can't send mail - it can only receive it. You need a SMTP client component (TidSMTP) to send mail. A mail server when it sends mail acts like an SMTP client.

Brier answered 14/3, 2010 at 13:11 Comment(1)
To expand on that, Indy has a TIdSMTPRelay component that is meant to be used on the server end to forward received emails to other servers.Unwept
E
15

Here's a complete example on how to send an email:

VAR SMTP : TIdSMTP;
VAR MSG : TIdMSG;
.
.
  MSG:=TIdMSG.Create(NIL);
  TRY
    WITH MSG.Recipients.Add DO BEGIN
      Name:='<Name of recipient>';
      Address:='<Email address of recipient>'
    END;
    MSG.BccList.Add.Address:='<Email address of Blind Copy recipient>';
    MSG.From.Name:='<Name of sender>';
    MSG.From.Address:='<Email address of sender>';
    MSG.Body.Text:='<Message Body>';
    MSG.Subject:='<Subject of message>';
    SMTP:=TIdSMTP.Create(NIL);
    TRY
      SMTP.Host:='x.x.x.x'; // IP Address of SMTP server
      SMTP.Port:=25; // Port address of SMTP service (usually 25)
      SMTP.Connect;
      TRY
        SMTP.Send(MSG)
      FINALLY
        SMTP.Disconnect
      END
    FINALLY
      SMTP.Free
    END
  FINALLY
    MSG.Free
  END;
.
.

(I know that WITH is frowned upon, but I generally use it in instances like this where there's no doubt as to what is going on, and where there's no (or just an infinitesimal) chance of ambiguity)

Esperance answered 14/3, 2010 at 8:10 Comment(1)
Wow - this code screams. I'd convert most of it to mixed-case or lowercase to make it friendlier for the eyes of most Delphi developers.Pathfinder
B
9

A SMTP server component can't send mail - it can only receive it. You need a SMTP client component (TidSMTP) to send mail. A mail server when it sends mail acts like an SMTP client.

Brier answered 14/3, 2010 at 13:11 Comment(1)
To expand on that, Indy has a TIdSMTPRelay component that is meant to be used on the server end to forward received emails to other servers.Unwept

© 2022 - 2024 — McMap. All rights reserved.