[DUG] Email/SMTP code

David O'Brien Dave at iccs.co.nz
Tue Jul 12 14:57:16 NZST 2011


v10.2.5

 

ASettings is an object I store all application settings in, the properties in this case are strings except SMTPPort (Integer or the like).

 

Don’t know if all these are required, I do a bit of experimentation occasionally:

IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase, IdSMTP, IdBaseComponent, IdMessage, IdAttachmentFile

 

EmailFailed is not required either (Global Var). I’d probably make this a function and return true/false now.

 

From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz] On Behalf Of John Bird
Sent: Tuesday, 12 July 2011 2:47 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] Email/SMTP code

 

Looks good.

 

Couple of quick questions:

 

Is this Indy10?   (Indy is notorious for changing properties/names etc between versions)

 

Is Asettings a TidSMTP component?   haven’t figured out yet...

 

Do you have a copy of the uses clause just in case – I think I have it sorted

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, IdBaseComponent, IdComponent,

  IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,

  IdMessageClient, IdSMTPBase, IdSMTP, IdMessage, IdAttachmentFile;

 

John


 

 

From: David O'Brien <mailto:Dave at iccs.co.nz>  

Sent: Tuesday, July 12, 2011 2:01 PM

To: NZ Borland Developers Group - Delphi List <mailto:delphi at delphi.org.nz>  

Subject: Re: [DUG] Email/SMTP code

 

Wrote this a while ago, but still works...

 

procedure SendEmail(msgFrom, msgTo, msgSubject: String; msgBody: TStringList; Attachments: TStringList) ;

var

  IdMessage: TIdMessage;

  SMTP: TIdSMTP;

  t: TDateTime ;

  i: Integer ;

begin

  try

    if ASettings.SMTPServer <> '' then

    begin

      SMTP := TidSMTP.Create(nil) ;

      IdMessage := TIdMessage.Create(SMTP);

      try

        idMessage.Clear ;

        idMessage.From.Address := msgFrom ;

        idMessage.Recipients.Add.Address := msgTo ;

        idMessage.Subject := msgSubject ;

        if pos('<html', lowercase(msgBody.Text)) <> 0 then

          idMessage.ContentType := 'text/html' ;

        if msgBody <> nil then

          IdMessage.Body.Assign(msgBody) ;

        t := now ;

        while (SMTP.Connected) and

          (now < t + 10 * (1/86400)) do // 10 Seconds

          sleep(10) ;

        if Assigned(Attachments) then

          for i := 0 to pred(Attachments.Count) do

            TIdAttachmentFile.Create(IdMessage.MessageParts, Attachments[i]) ;

        SMTP.Host := ASettings.SMTPServer ;

        SMTP.Port := ASettings.SMTPPort ;

        SMTP.Username := ASettings.Username ;

        SMTP.Password := ASettings.Password ;

        SMTP.Connect ;

        try

          SMTP.Send(idMessage) ;

        finally

          SMTP.Disconnect ;

        end ;

      finally

        SMTP.Free ;

        IdMessage.Free ;

      end;

    end;

  except

    EmailFailed := True ;

  end ;

end ;

 

From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz] On Behalf Of John Bird
Sent: Tuesday, 12 July 2011 1:47 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] Email/SMTP code

 

Anyone got a recommendation for the best (free) code/samples to drive Indy10 for sending an email with attachment

 

-Indy10.2.3

-Send attachment

-SMTP server and email addresses will be known

 

I have tried a couple (eg AtoZed SendMail example) but it seems to time out connecting to the SMTP 

 

John

 

________________________________

_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject: unsubscribe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20110712/f171f25e/attachment-0001.html 


More information about the Delphi mailing list