Page 1 of 1

SendSmtpMail always fails

Posted: Tue Sep 21, 2021 6:27 am
by MarkusD
Hi,

I want to send mails with SendSmtpMail. But I always get "false" as the result.

Code: Select all

    var FileNames : TStringDynArray := Utils.GetFiles(gConstData.ExchangeFolder, faAnyFile, 0, '*.txt');
    for var i : Integer := 0 to High(FileNames) do begin
      var SL := TStringList.Create;
      var MailFrom, MailSubject, MailAddr, MailBody, SmtpServer, SmtpPassword : string;
      var SmtpPort : Integer;
      var FileName : string := FileNames[i];
      SL.LoadFromFile(FileName);
      for var j : Integer := 0 to Pred(SL.Count) do
        Log.Debug('[%d]: [%s]', [j, SL[j]]);

      MailFrom     := gConstData.AMMailIniFile.ReadString(sPaths, 'MailFrom', '');
      SmtpServer   := gConstData.AMMailIniFile.ReadString(sPaths, 'SmtpServer', '');
      SmtpPassword := gConstData.AMMailIniFile.ReadString(sPaths, 'SmtpPassword', '');
      SmtpPort     := gConstData.AMMailIniFile.ReadInteger(sPaths, 'SmtpPort', 0);

      MailSubject  := SL[0];
      MailSubject  := 'Betreff';                                                                // Debug ###############

      MailAddr     := SL[1];
      MailAddr     := 'status@example.org';                                              // Debug ###############

      MailBody     := StringReplace(SL[9], '#13#10#', sLineBreak, [rfReplaceAll]);
      MailBody     := 'Body';                                                                   // Debug ###############

      Log.Debug('MailFrom:     [%s]', [MailFrom]);
      Log.Debug('MailAddr:     [%s]', [MailAddr]);
      Log.Debug('MailSubject:  [%s]', [MailSubject]);
      Log.Debug('MailBody:     [%s]', [MailBody]);
      Log.Debug('SmtpServer:   [%s]', [SmtpServer]);
//      Log.Debug('SmtpPassword: [%s]', [SmtpPassword]);
      Log.Debug('SmtpPort:     [%d]', [SmtpPort]);

      Log.Info(Format('Send Mail [%s] from [%s] to [%s]; SmtpServer [%s]; SmtpPort [%d]',
          [MailSubject, MailFrom, MailAddr, SmtpServer, SmtpPort]));
      if not SendSmtpMail(
          MailFrom,
          MailAddr,
          '',              // replyTo
          MailSubject,
          MailBody,
          nil,             // attachments
          SmtpServer,
          '',              // authUserName
          SmtpPassword,    // authPassword
          SmtpPort,        // port
          True,            // ssl
          True,            // tls
          0,               // parentWindow
          false,           // hidden
          false,           // background
          nil) then begin  // settings
        Log.Error('Error sending mail!');
      end;
Is there any way to see what exactly went wrong? Any status variables?

I'm not sure what to do with the last parameter „settings“?

I'm using this in a windows service, so there is no GUI, but that should make no difference. TIA, Markus

Re: SendSmtpMail always fails

Posted: Tue Sep 21, 2021 10:39 am
by madshi
Does madExcept succeed sending emails via SMTP if it's done as part of the exception box in a GUI application? In other words: Is it only failing if you call SendSmtpMail manually, while it succeeds when madExcept calls this internally? Or does madExcept generally fail sending any SMTP emails at all?

You could debug this by copying madExcept.pas + mad.inc into your project folder, then set a breakpoint in madExcept.pas in "TSmtp.SendMail" and step through the code to see where it fails.

Re: SendSmtpMail always fails

Posted: Wed Sep 22, 2021 5:41 am
by MarkusD
madshi wrote: Tue Sep 21, 2021 10:39 am Does madExcept succeed sending emails via SMTP if it's done as part of the exception box in a GUI application? In other words: Is it only failing if you call SendSmtpMail manually, while it succeeds when madExcept calls this internally? Or does madExcept generally fail sending any SMTP emails at all?

You could debug this by copying madExcept.pas + mad.inc into your project folder, then set a breakpoint in madExcept.pas in "TSmtp.SendMail" and step through the code to see where it fails.
Easy solution in this case. I forgot the parameter "authUserName". Now it works, in the windows service application without a GUI too.

Code: Select all

if not SendSmtpMail(
          FMailFrom,       // mailFrom
          MailAddr,        // rcptTo
          '',              // replyTo
          MailSubject,     // subject
          MailBody,        // body
          nil,             // attachments
          FSmtpServer,     // server
          FMailFrom,       // authUserName
          FSmtpPassword,   // authPassword
          FSmtpPort,       // port
          True,            // ssl
          True,            // tls
          0,               // parentWindow
          true,            // hidden
          false,           // background
          nil) then begin  // settings
        Log.Error('Error sending mail!');
      end;