SendEmail with Indy components

I am trying to send an email, but I have a problem, however I found this code on the Internet:

Uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdMessage, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, IdBaseComponent, IdComponent, IdIOHandler, IdExplicitTLSClientServerBase, IdSMTPBase procedure SendSimpleMail; var Msg: TIdMessage; DestAddr: TIdEmailAddressItem; begin Msg := TIdMessage.Create(Self); //error here Msg.From.Text := 'name'; Msg.From.Address := ' username@gmail.com '; Msg.Subject := 'Test'; DestAddr := Msg.Recipients.Add; DestAddr.Text := 'name'; DestAddr.Address := ' username@yahoo.com '; Msg.Body.Add('simple test mail.'); tIdSMTP.Host := 'smtp.gmail.com'; tIdSMTP.Port := 25; tIdSMTP.AuthenticationType := atLogin; //error here (2 error) tIdSMTP.Username := ' username@gmail.com '; tIdSMTP.Password := 'password'; tIdSMTP.Connect; tIdSMTP.Authenticate; tIdSMTP.Send(Msg); tIdSMTP.Disconnect; end; 

But, however, I noticed a lot of errors, and I am missing the Indy component. Compiler Errors:

 [DCC Error] Unit1.pas(36): E2003 Undeclared identifier: 'Self' [DCC Error] Unit1.pas(46): E2233 Property 'Host' inaccessible here [DCC Error] Unit1.pas(47): E2233 Property 'Port' inaccessible here [DCC Error] Unit1.pas(48): E2003 Undeclared identifier: 'AuthenticationType' [DCC Error] Unit1.pas(48): E2003 Undeclared identifier: 'atLogin' [DCC Error] Unit1.pas(49): E2233 Property 'Username' inaccessible here [DCC Error] Unit1.pas(50): E2233 Property 'Password' inaccessible here [DCC Error] Unit1.pas(51): E2076 This form of method call only allowed for class methods [DCC Error] Unit1.pas(52): E2076 This form of method call only allowed for class methods [DCC Error] Unit1.pas(53): E2076 This form of method call only allowed for class methods [DCC Error] Unit1.pas(54): E2076 This form of method call only allowed for class methods [DCC Error] Project1.dpr(5): F2063 Could not compile used unit 'Unit1.pas' 

Thanks for the help in advance.

+7
source share
2 answers

The code from your question was written for Indy 9 and from your compiler error it seems that you are using Indy 10. To your compiler errors:

  • Undeclared identifier: Self - Self is a pointer to an instance of a class, and since you are not writing SendSimpleMail as a class method, but as a separate procedure, you do not have Self just because you do not have a class. A class method that you could write, for example, for your form class, for example, for example. TForm1.SendSimpleMail , where inside this Self method will have the value of the TForm1 instance, the form itself.

  • And the rest of the errors you got because you were TIdSMTP class, not the object instance. A commonly used practice is to declare a local variable, create an instance of an object that assigns it to this variable, work with this object (variable), and free an instance of the object.

I would try something like this (tested with Indy 10 since Delphi 2009):

 uses IdSMTP, IdMessage, IdEMailAddress; procedure SendSimpleMail; var IdSMTP: TIdSMTP; IdMessage: TIdMessage; IdEmailAddressItem: TIdEmailAddressItem; begin IdSMTP := TIdSMTP.Create(nil); try IdSMTP.Host := 'smtp.gmail.com'; IdSMTP.Port := 25; IdSMTP.AuthType := satDefault; IdSMTP.Username := ' username@gmail.com '; IdSMTP.Password := 'password'; IdSMTP.Connect; if IdSMTP.Authenticate then begin IdMessage := TIdMessage.Create(nil); try IdMessage.From.Name := 'User Name'; IdMessage.From.Address := ' username@gmail.com '; IdMessage.Subject := 'E-mail subject'; IdMessage.Body.Add('E-mail body.'); IdEmailAddressItem := IdMessage.Recipients.Add; IdEmailAddressItem.Address := ' recipient@email.com '; IdSMTP.Send(IdMessage); finally IdMessage.Free; end; end; IdSMTP.Disconnect; finally IdSMTP.Free; end; end; 
+8
source

For google smtp you need to use TLS or SSL! http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

An example of your procedure is the entry for INDY9, and if you use INDY10, this cannot be compiled. You need to make adjustments.

0
source

All Articles