Search code examples
emailfastreportdelphi-10.4-sydney

Using TfrxMailExport from Fast Reports


I've been having a problem while using Fast Reports in Delphi, The Object I'm using is TfrxMailExport, The problem I'm facing is that the values of the email server aren't getting filled properly.

The Code:

email := TfrxMailExport.Create(self);
email.Subject := 'Teste';
email.Lines.Clear;
email.Lines.Add('Linha 1');
email.Lines.Add('Linha 2');
email.Lines.Add('Linha 3');
email.Lines.Add('Linha 4');
email.Address := '[email protected]';
email.SmtpHost := '0.0.0.0';
email.SmtpPort := 25;
email.FromMail := '[email protected]';
email.FromName := 'NAME';
email.Login := 'Login';
email.Password := 'Password';

email.TimeOut := 30;
email.Report := Rela;

rela.Export(email);
email.Destroy;

Only the E-Mail side gets filled


Solution

  • Since the post is not likely to get an answer (if there is one), I'm gonna post my workaround as a solution in case someone is having the same problem.

    I created a form similar to the one in Fast Reports, I export the FR file to PDF, this one works fine.

    Procedure SomeProc();
    var pdf : TfrxPDFExport;
    begin
      pdf := TfrxPDFExport.Create(self);
      pdf.Compressed := True;
      pdf.EmbeddedFonts := False;
      pdf.Background := True;
      pdf.PrintOptimized := False;
      pdf.Outline := False;
      pdf.Transparency := False;
      pdf.Quality := 95;
      pdf.ProtectionFlags := [eModify, eCopy, eAnnot];
      pdf.OpenAfterExport := False;
      pdf.ShowProgress := False;
      pdf.ShowDialog := false;
      pdf.FileName := 'C:\SomeFolder\'+fileName+'.pdf';
      pdf.HideToolbar := False;
      pdf.HideMenubar := False;
      pdf.HideWindowUI := False;
      pdf.FitWindow := False;
      pdf.CenterWindow := False;
      pdf.PrintScaling := False;
      myReport.Export(pdf);
    end;
    

    Then for the email, I used a C# .Net DLL, and called it from the Delphi application.

    The C# code:

    using RGiesecke.DllExport;
    
    [DllExport("SendEmail", CallingConvention = CallingConvention.StdCall)]
     public static string SendEmail(string txtTo, string txtToCC, string txtToBCC, string txtSubject, string txtMessage, string txtFrom, string txtServer, string txtPort, string txtUtilizador, string txtPasse, string txtFile ,bool cbSSL)
     {
         MailMessage message = new MailMessage();
         SmtpClient smtpClient = new SmtpClient();
    
         string msg = string.Empty;
         try
         {
             MailAddress fromAddress = new MailAddress(txtFrom);
             Attachment attachment = new Attachment(txtFile, System.Net.Mime.MediaTypeNames.Application.Pdf);
    
             message.From = fromAddress;                
             message.To.Add(txtTo);
             if (txtToCC != "")
                 message.CC.Add(txtToCC);
             if (txtToBCC != "")
                 message.Bcc.Add(txtToBCC);
    
             message.Attachments.Add(attachment);
    
             message.Subject = txtSubject;
    
             message.IsBodyHtml = true;
             message.Body = txtMessage;
             smtpClient.Timeout = 5000;
             smtpClient.Host = txtServer; 
             int.TryParse(txtPort, out int port);
             smtpClient.Port = port;
    
             smtpClient.UseDefaultCredentials = false;
             smtpClient.Credentials = new System.Net.NetworkCredential(txtUtilizador, txtPasse);
             smtpClient.EnableSsl = cbSSL;
             smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    
             smtpClient.Send(message);
             message.Dispose();
             return "Message Sent.";
         }
         catch (Exception ex)
         {
             return ex.Message;
         }
     }
    

    And to finish, calling it in Delphi.

    function SendEmail(txtTo, txtToCC, txtToBCC, txtSubject, txtMessage, txtFrom, txtServer, txtPort, txtUtilizador, txtPasse, txtFicheiro : PAnsiChar; cbSSL : Boolean) : PAnsiChar; stdcall; external 'SendEmail.dll';
    
    
    showmessage(SendEmail(PAnsiChar(AnsiString('To'))
                   , PAnsiChar(AnsiString('ToCC'))
                   , PAnsiChar(AnsiString('ToBCC'))
                   , PAnsiChar(AnsiString('Subject'))
                   , PAnsiChar(AnsiString('Message'))
                   , PAnsiChar(AnsiString('From'))
                   , PAnsiChar(AnsiString('Server'))
                   , PAnsiChar(AnsiString('Port'))
                   , PAnsiChar(AnsiString('User'))
                   , PAnsiChar(AnsiString('Pass'))
                   , PAnsiChar(AnsiString('File'))
                   , SSL));
    

    I made a couple mistakes, like using a function to convert the strings without having to write each one, it doesn't work for some reason. The function needs to specify it's a stdcall.