Hi all,

I want to send automatic mail from my Delphi application with attachment without invocation of any client tool like outlook. Actually My application runs as a windows service and every 5 minute generate some response files. I want those files to be mailed automatically. But how do I do that?

thanks n regards
Jeet

Indy has an SMTP client you can use to send emails. Here is a sample:

function TEmailFm.SendTestEmail(const aString: String): Boolean;
Var
  aMsg: TIdMessage;
  aFile: String;
begin

  Result := True;
  aStringList := TStringList.Create;

  try
    aMsg := TIdMessage.Create(Self);
    aMsg.From.Address := Cfg.eSourceEmail;
    aMsg.Subject := 'Test E-Mail';
    aMsg.Recipients.EMailAddresses := aString;
    aMsg.Body.Text := 'Test E-Mail.';
    IdSMTP1.Host := Cfg.eServerIp;
    IdSMTP1.Port := Cfg.eServerPort;
    if (Cfg.eAuthType = 'atLogin') then
    begin
      IdSMTP1.UserId := Cfg.eLogin;
      IdSMTP1.Password := Cfg.ePassword;
      IdSMTP1.AuthenticationType := atLogin;
    end else
    begin
      IdSMTP1.AuthenticationType := atNone;
    end;
    IdSMTP1.Connect;
    IdSMTP1.Send(aMsg);
    IdSMTP1.Disconnect;
    if IdSMTP1.Connected then
      IdSMTP1.Disconnect;
  except
    on E: Exception do
    begin
      MessageDlg('Error sending test e-mail.'#13#10 + E.Message, mtError, [mbOk], E.HelpContext);
      Result := False;
    end;
  end;

  aFile := IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0))) + 'TestEmail.log';
  if FileExists(aFile) then DeleteFile(aFile);
  if (aStringList.Count > 0) then aStringList.SaveToFile(aFile);


  FreeAndNil(aStringList);
  FreeAndNil(aMsg);
end;

Indy has an SMTP client you can use to send emails. Here is a sample:

function TEmailFm.SendTestEmail(const aString: String): Boolean;
Var
  aMsg: TIdMessage;
  aFile: String;
begin

  Result := True;
  aStringList := TStringList.Create;

  try
    aMsg := TIdMessage.Create(Self);
    aMsg.From.Address := Cfg.eSourceEmail;
    aMsg.Subject := 'Test E-Mail';
    aMsg.Recipients.EMailAddresses := aString;
    aMsg.Body.Text := 'Test E-Mail.';
    IdSMTP1.Host := Cfg.eServerIp;
    IdSMTP1.Port := Cfg.eServerPort;
    if (Cfg.eAuthType = 'atLogin') then
    begin
      IdSMTP1.UserId := Cfg.eLogin;
      IdSMTP1.Password := Cfg.ePassword;
      IdSMTP1.AuthenticationType := atLogin;
    end else
    begin
      IdSMTP1.AuthenticationType := atNone;
    end;
    IdSMTP1.Connect;
    IdSMTP1.Send(aMsg);
    IdSMTP1.Disconnect;
    if IdSMTP1.Connected then
      IdSMTP1.Disconnect;
  except
    on E: Exception do
    begin
      MessageDlg('Error sending test e-mail.'#13#10 + E.Message, mtError, [mbOk], E.HelpContext);
      Result := False;
    end;
  end;

  aFile := IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0))) + 'TestEmail.log';
  if FileExists(aFile) then DeleteFile(aFile);
  if (aStringList.Count > 0) then aStringList.SaveToFile(aFile);


  FreeAndNil(aStringList);
  FreeAndNil(aMsg);
end;

Hi Friend,

Thanks for the reply, I am using two components of Indy 10.1.5 and Delphi 2006. For attachment of file I am using TIdMessage and the other control is TIdSMTP, despite of giving all correct information, I am getting invalid host error socket error 11001. I have used IP address of my SMTP also. But still i am getting socket error 11001. I dont know WHY?
Please shed some light.

thanks
Jeet

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.