[DUG] Email/SMTP code

David O'Brien Dave at iccs.co.nz
Thu Jul 14 19:12:18 NZST 2011


Put the code in another project that doesn’t require admin access?

What server are you pointing at, and are you going through a proxy server?

 

Username & password are both blank in my implementation.

 

Here is a bit more up to date version from another project, this returns any error message.

function SendEmail(msgFrom, msgTo, msgCC, msgBCC, msgSubject: String; msgBody: TStringList; Attachments: TStringList): String ;

var

  IdMessage: TIdMessage;

  SMTP: TIdSMTP;

  t: TDateTime ;

  i: Integer ;

begin

  Result := '' ;

  try

    if ASettings.SMTPServer <> '' then

    begin

      SMTP := TidSMTP.Create(nil) ;

      IdMessage := TIdMessage.Create(SMTP);

      try

        MsgTo := Trim(MsgTo) ;

        if Length(MsgTo) > 0 then

          if MsgTo[Length(MsgTo)] = ';' then

            Delete(MsgTo, Length(MsgTo), 1) ;

        MsgCC := Trim(MsgCC) ;

        if Length(MsgCC) > 0 then

          if MsgCC[Length(MsgCC)] = ';' then

            Delete(MsgCC, Length(MsgCC), 1) ;

        MsgBCC := Trim(MsgBCC) ;

        if Length(MsgBCC) > 0 then

          if MsgBCC[Length(MsgBCC)] = ';' then

            Delete(MsgBCC, Length(MsgBCC), 1) ;

        idMessage.Clear ;

        idMessage.From.Address := msgFrom ;

        if MsgTo <> '' then

          idMessage.Recipients.Add.Address := msgTo ;

        if MsgCC <> '' then

          idMessage.CCList.Add.Address := msgCC ;

        if MsgBCC <> '' then

          idMessage.BccList.Add.Address := msgBCC ;

        idMessage.Subject := msgSubject ;

        with TIdText.Create(IdMessage.MessageParts, nil) do

        begin

          Body.Assign(msgBody) ;

          ContentType := 'text/html';

        end;

        if Assigned(Attachments) then

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

            with TIdAttachmentFile.Create(idMessage.MessageParts, Attachments[i]) do

            begin

              ContentID := inttoStr(1000+i) ;

              ContentType := 'image/jpeg' ;

              Filename := ExtractFileName(Attachments[i]) ;

            end;

        t := now ;

        while (SMTP.Connected) and

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

          sleep(10) ;

        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 ;

      end;

    end;

  except

    on e: Exception do

    begin

      Result := msgTo+': '+e.Message ;

      EmailFailed := True ;

    end;

  end ;

end ;

 

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

 

Trying to debug this SMTP program, having tweaked the code a little – I am getting “Connection timed out” 

 

However I cannot debug, as on Windows 7 this program requires admin access.

 

I restarted the IDE (BDS.EXE) running as administrator but still get the error message

 

“Cannot create process”   (Debug)

 

or

 

“Access denied”  (Run without debugger).

 

Can run the program outside of the IDE by right clicking and selecting “Run as administrator” but there is no debugging then!

 

What have I missed?

 

John

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


More information about the Delphi mailing list