Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2005.03.13;
Скачать: CL | DM;

Вниз

Помогите написать корректный constructor   Найти похожие ветки 

 
SHort   (2005-02-26 09:08) [0]

При выполнении конструктора вываливается ошибка - код модуля

unit UMailSend;

interface
uses
 Windows, Messages, SysUtils, Classes, Controls,
 IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP,    
 IdComponent, IdUDPBase, IdUDPClient, IdDNSResolver,
 IdBaseComponent, IdMessage,
 StdCtrls, ExtCtrls, ComCtrls, IdAntiFreezeBase, IdAntiFreeze;
type
TMSender = class
  fMailServers:String;
private
  IdMessage:TIdMessage;
  IdSMTP:TIdSMTP;
public
  function SendMail(addr_host,addr_sender,addr_Recipients,Subj,Body:String):boolean;
  Constructor Create;
  end;

implementation

Constructor TMSender.Create;
begin
TIdSMTP.Create(IdSMTP);
TIdMessage.Create(IdMessage);
end;

function TMSender.SendMail(addr_host,addr_sender,addr_Recipients,Subj,Body:String):boolean;
var
LBody:TStringList;
begin

result:=true;
LBody:=TStringList.Create;
LBody.Add(Body);

IdMessage.From.Text := addr_sender;
IdMessage.Sender.Text := addr_sender;
IdMessage.Recipients.EMailAddresses := addr_Recipients;
IdMessage.Subject := Subj;
IdMessage.Body := LBody;
fMailServers:=addr_host;

with IdSMTP do
  begin
  Host := fMailServers;
  try
  Connect;
  Send(IdMessage);
  Disconnect;
  Result := true;
  except on E : Exception do
    begin
    if connected then try disconnect; except end;
    Result:= false;
    end;
  end;
  end;
end;
end.


 
MBo ©   (2005-02-26 09:12) [1]

>TIdSMTP.Create(IdSMTP);
так объекты не создают ;)
должно быть
FIdSMTP:=TIdSMTP.Create(Self);
и второе аналогично
F я добавил, поскольку у тебя имя субкомпонента совпадает с именем модуля в Uses


 
jack128 ©   (2005-02-26 09:53) [2]

MBo ©   (26.02.05 9:12) [1]
>TIdSMTP.Create(IdSMTP);
так объекты не создают ;)

создают. Только потом не могут использывать ;-)
FIdSMTP:=TIdSMTP.Create(nil);
и не забыть в деструкторе уничтожить этот FIdSMTP

SHort   (26.02.05 9:08)
При выполнении конструктора вываливается ошибка - код модуля

При выполнении или при компиляции??


 
SHort   (2005-02-26 10:19) [3]

Ошибка выполнения. В данный момент (после исправления FIdSMTP:=TIdSMTP.Create(nil);)- "Stack overflow" возникает после выполнения FIdMessage:=TIdMessage.Create(nil);

в случае FIdSMTP:=TIdSMTP.Create(Self); - ошибка компиляции - Incompatible types: "TComponent" and "TMSender"


 
MBo ©   (2005-02-26 10:25) [4]

>FIdSMTP:=TIdSMTP.Create(Self);
Это моя ошибка, правильно с nil, как у jack128.

Приведи полный текст после исправлений


 
SHort   (2005-02-26 10:30) [5]

unit UMailSend;

interface
uses
 Windows, Messages, SysUtils, Classes, Controls,
 IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, IdComponent,
 IdUDPBase, IdUDPClient, IdDNSResolver, IdBaseComponent, IdMessage,
 StdCtrls, ExtCtrls, ComCtrls, IdAntiFreezeBase, IdAntiFreeze;
type
  TMSender = class

private
  MIdMessage:TIdMessage;
  MIdSMTP:TIdSMTP;
public
  fMailServers:String;
  function SendMail(addr_host,addr_sender,addr_Recipients,Subj,Body:String):boolean;
  Constructor Create;
  {destructor Destroy;}
end;

implementation

Constructor TMSender.Create;
begin
MIdSMTP:=TIdSMTP.Create(nil);
MIdMessage:=TIdMessage.Create(nil);
end;

{destructor Destroy;
begin
MIdSMTP.Free;
MIdMessage.Free;
end;}

function TMSender.SendMail(addr_host,addr_sender,addr_Recipients,Subj,Body:String):boolean;
var
LBody:TStringList;
begin
//===============
result:=true;
LBody:=TStringList.Create;
LBody.Add(Body);
//IdMessage.Create();
MIdMessage.From.Text := addr_sender;
MIdMessage.Sender.Text := addr_sender;
MIdMessage.Recipients.EMailAddresses := addr_Recipients;
MIdMessage.Subject := Subj;
MIdMessage.Body := LBody;
fMailServers:=addr_host;
//88888888888
with MIdSMTP do
  begin
  Host := fMailServers;
  try
  Connect;
  Send(MIdMessage);
  Disconnect;
  Result := true;
  except on E : Exception do
    begin
    if connected then try disconnect; except end;
    Result:= false;
    end;
  end;
  end;
end;
end.


 
MBo ©   (2005-02-26 10:44) [6]

У меня все нормально, покажи, как используешь

немного подправил:

unit UMailSend;

interface
uses
 SysUtils, Classes, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP,
   IdComponent,
 IdUDPBase, IdUDPClient, IdDNSResolver, IdBaseComponent, IdMessage,
 IdAntiFreezeBase, IdAntiFreeze;
type
 TMSender = class
 private
   MIdMessage: TIdMessage;
   MIdSMTP: TIdSMTP;
 public
   fMailServers: string;
   function SendMail(addr_host, addr_sender, addr_Recipients, Subj, Body:
     string): boolean;
   constructor Create;
   destructor Destroy; override;
 end;

implementation

constructor TMSender.Create;
begin
 MIdSMTP := TIdSMTP.Create(nil);
 MIdMessage := TIdMessage.Create(nil);
end;

destructor TMSender.Destroy;
begin
 MIdSMTP.Free;
 MIdMessage.Free;
end;

function TMSender.SendMail(addr_host, addr_sender, addr_Recipients, Subj, Body:
 string): boolean;
begin
 result := true;
 MIdMessage.From.Text := addr_sender;
 MIdMessage.Sender.Text := addr_sender;
 MIdMessage.Recipients.EMailAddresses := addr_Recipients;
 MIdMessage.Subject := Subj;
 MIdMessage.Body.Add(Body);
 fMailServers := addr_host;
 with MIdSMTP do begin
   Host := fMailServers;
   try
     Connect;
     Send(MIdMessage);
     Disconnect;
     Result := true;
   except on E: Exception do begin
       if connected then try
         disconnect;
       except
       end;
       Result := false;
     end;
   end;
 end;
end;
end.



 
SHort   (2005-02-26 11:00) [7]

program Project1;

uses
 Windows,SysUtils,
 USpars in "USpars.pas",
 UMailSend in "UMailSend.pas";
{$R *.res}
Var i,k:integer;
       S1,S2,S3,S4,S5:string;
       MSender:TMSender;
begin
i:=ParamCount;
if i<>5 then halt
else
 begin
  S1:=LowerCase(ParamStr(1));
  S2:=LowerCase(ParamStr(2));
  S3:=LowerCase(ParamStr(3));
  S4:=LowerCase(ParamStr(4));
  S5:=LowerCase(ParamStr(5));
 end;
if StrPars("/HOST=",S1)="" then halt
   else S1:=StrPars("/HOST=",S1);
if StrPars("/SEND=",S2)="" then halt
   else S2:=StrPars("/SEND=",S2);
if StrPars("/Recipient=",S3)="" then halt
   else S3:=StrPars("/Recipient=",S3);
if StrPars("/SUBJ=",S4)="" then halt
   else S4:=StrPars("/SUBJ=",S4);
if StrPars("/BODY=",S5)="" then halt
   else S5:=StrPars("/BODY=",S5);

MSender.Create;
MSender.SendMail(S1, S2,S3,S4,S5);

end.

При трассировке вываливается ошибка "Acess violation at address XXXXXXX in module Project1.exe. Read of address XXXXXXX" при выполнении MIdMessage := TIdMessage.Create(nil);


 
Anton_K ©   (2005-02-26 11:03) [8]

Может, в конструктор inherited вставить стоит?


 
MBo ©   (2005-02-26 11:09) [9]

>MSender.Create;
Ну неужели на одни грабли нравится многократно наступать?


 
SHort   (2005-02-26 11:21) [10]

Все, понял (MSender:=TMSender.Create;). Большое спасибо!!!



Страницы: 1 вся ветка

Текущий архив: 2005.03.13;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.028 c
1-1109585518
RavenD
2005-02-28 13:11
2005.03.13
Версия EXE из DLL?


1-1109243441
Kostik__
2005-02-24 14:10
2005.03.13
INI-файлы


1-1109665524
Ivolg
2005-03-01 11:25
2005.03.13
TrayIcon


1-1109391605
nap
2005-02-26 07:20
2005.03.13
+,-,*,/


1-1109343363
Aleks
2005-02-25 17:56
2005.03.13
Как из своей программы написать сообщение на рабочем столе