Recent

Author Topic: Using synapse to send email  (Read 315 times)

hamacker

  • Jr. Member
  • **
  • Posts: 58
Using synapse to send email
« on: September 12, 2024, 11:52:21 pm »
Guys, I'm following the path of Synapse and porting some Delphinian code to it.
But I'm encountering problems, the code is here:
https://pastebin.com/g1dRH4rQ

It has three methods, all for attaching files that no longer exist.
I keep looking at codes on the internet, but they seem to be old and not in tune with the current ones.
Does anyone have something similar or could indicate why these three lines that I underlined in the source no longer work?

rvk

  • Hero Member
  • *****
  • Posts: 6388
Re: Using synapse to send email
« Reply #1 on: September 13, 2024, 02:01:56 pm »
It has three methods, all for attaching files that no longer exist.
Sure they do. But you declared MimeMsg as TMimePart.
That's something different from TMimeMess which does have the AddPartBinary and AddTextPart.

You need to create a TMimeMess and set the mail properties.
After that you need to add TMimePart to separate certain mail parts (with boundaries).
So you need to do something like
For the mimetype... you could do something like:
Code: Pascal  [Select][+][-]
  1. P := MimeMsg.AddPartMultipart('mixed', nil);

After that you can use AddPartText and AddPartBinary and pass P with it as parent part.

BTW. AddPartText also expects a TStrings, not a String.

You can search for TMimeMess on this forum. There are many examples.

(For example https://forum.lazarus.freepascal.org/index.php/topic,49869.msg362756.html#msg362756 but there are many more)

Also don't forget the  MimeMsg.EncodeMessage before sending the mail.
« Last Edit: September 13, 2024, 02:05:50 pm by rvk »

hamacker

  • Jr. Member
  • **
  • Posts: 58
Re: Using synapse to send email
« Reply #2 on: September 13, 2024, 03:50:53 pm »
Excelent tips.
I think that I got it! I sent my first email using synapse.
My code now for help others:
Code: Pascal  [Select][+][-]
  1. unit utils_email;
  2.  
  3. interface
  4.  
  5. { Sample:
  6. with TUtilsEMailSender.Create do
  7. begin
  8.   Host := 'localhost';
  9.   Port := 587;
  10.   Username := 'me@domain.com';
  11.   Password := 'secret';
  12.   From := 'me@domain.com';
  13.   Recipients.Add('johdoe@domain.com');
  14.   CC.Text:='cc1@domain.com'+sLineBreak+'cc2@domain.com';
  15.   BCC.Text:='bcc1@domain.com'+sLineBreak+'bcc2@domain.com';
  16.   Subject := edtSubject.Text;
  17.   Body := edtBody.Text;
  18.   //Attachments.Text := 'attach1.ods' + sLineBreak + 'attach2.html';
  19.   Attachments.Add('attach1.ods');
  20.   Attachments.Add('attach2.html');
  21.   Result := Send;
  22.   if Result = '' then
  23.     ShowMessage('SUCESS')
  24.   else
  25.     ShowMessage('FAIL');
  26.   Free;
  27. end;
  28. }
  29.  
  30.  
  31. uses
  32.   Classes, SysUtils;
  33.  
  34. type
  35.   TUtilsEMailSender = class
  36.   private
  37.     FHost: string;
  38.     FPort: Integer;
  39.     FUsername: string;
  40.     FPassword: string;
  41.     FFrom: string;
  42.     FRecipients: TStrings;
  43.     FCC: TStrings;
  44.     FBCC: TStrings;
  45.     FSubject: string;
  46.     FBody: string;
  47.     FAttachments: TStringList;
  48.  
  49.   public
  50.     constructor Create;
  51.     destructor Destroy; override;
  52.  
  53.     function Send: String;
  54.  
  55.     property Host: string read FHost write FHost;
  56.     property Port: Integer read FPort write FPort;
  57.     property Username: string read FUsername write FUsername;
  58.     property Password: string read FPassword write FPassword;
  59.     property From: string read FFrom write FFrom;
  60.     property Recipients: TStrings read FRecipients write FRecipients;
  61.     property CC: TStrings read FCC write FCC;
  62.     property BCC: TStrings read FBCC write FBCC;
  63.     property Subject: string read FSubject write FSubject;
  64.     property Body: string read FBody write FBody;
  65.     property Attachments: TStringList read FAttachments write FAttachments;
  66.   end;
  67.  
  68. implementation
  69.  
  70. uses
  71.   smtpsend, mimemess, mimepart;
  72.  
  73. { TUtilsEMailSender }
  74.  
  75. constructor TUtilsEMailSender.Create;
  76. begin
  77.   FRecipients := TStringList.Create;
  78.   FCC := TStringList.Create;
  79.   FBCC := TStringList.Create;
  80.   FAttachments := TStringList.Create;
  81. end;
  82.  
  83. destructor TUtilsEMailSender.Destroy;
  84. begin
  85.   FRecipients.Free;
  86.   FCC.Free;
  87.   FBCC.Free;
  88.   FAttachments.Free;
  89.   inherited;
  90. end;
  91.  
  92. function TUtilsEMailSender.Send: String;
  93. var
  94.   vSMTP: TSMTPSend;
  95.   vMimeMsg: TMimeMess;
  96.   vMimePart: TMimePart;
  97.   vTextPart: TStrings;
  98.   vFileStream: TFileStream;
  99.   i: Integer;
  100. begin
  101.   Result := '';
  102.   vSMTP := TSMTPSend.Create;
  103.   vMimeMsg := TMimeMess.Create;
  104.   if (Pos('@', FFrom)<=0) then
  105.     Result:='Você não informou o remetente.';
  106.   if (Result='') and (Pos('@', FRecipients.Text)<=0)  then
  107.     Result:='Você não informou o destinatario.';
  108.   if (Result='') and (Trim(FSubject)='')  then
  109.     Result:='Você não informou o assunto.';
  110.   if (Result='') and (Trim(FBody)='')  then
  111.     Result:='Você não informou o conteudo da mensagem.';
  112.   try
  113.     if Result='' then
  114.     begin
  115.       try
  116.         // Configuração do servidor SMTP
  117.         vSMTP.TargetHost := FHost;
  118.         vSMTP.TargetPort := IntToStr(FPort);
  119.         vSMTP.Username := FUsername;
  120.         vSMTP.Password := FPassword;
  121.  
  122.         // Configuração do e-mail
  123.         vMimeMsg.Header.From := FFrom;
  124.         for i := 0 to FRecipients.Count - 1 do
  125.           vMimeMsg.Header.ToList.Add(FRecipients[i]);
  126.         for i := 0 to FCC.Count - 1 do
  127.           vMimeMsg.Header.CCList.Add(FCC[i]);
  128.         vMimeMsg.Header.Subject := FSubject;
  129.  
  130.         // Criação da parte mista (para separar corpo e anexos)
  131.         vMimePart := vMimeMsg.AddPartMultipart('mixed', nil);
  132.  
  133.         // Adicionar corpo do e-mail como texto
  134.         vTextPart := TStringList.Create;
  135.         try
  136.           vTextPart.Text := FBody;
  137.           vMimeMsg.AddPartText(vTextPart, vMimePart);
  138.         finally
  139.           vTextPart.Free;
  140.         end;
  141.  
  142.         // Adicionar anexos
  143.         for i := 0 to FAttachments.Count - 1 do
  144.         begin
  145.           vFileStream := TFileStream.Create(FAttachments[i], fmOpenRead or fmShareDenyWrite);
  146.           try
  147.             vMimeMsg.AddPartBinary(vFileStream, ExtractFileName(FAttachments[i]), vMimePart);
  148.           finally
  149.             vFileStream.Free;
  150.           end;
  151.         end;
  152.  
  153.         // Conectar ao servidor SMTP e enviar o e-mail
  154.         if vSMTP.Login then
  155.         begin
  156.           vSMTP.MailFrom(FFrom, Length(FBody));
  157.           for i := 0 to FRecipients.Count - 1 do
  158.             vSMTP.MailTo(FRecipients[i]);
  159.           for i := 0 to FCC.Count - 1 do
  160.             vSMTP.MailTo(FCC[i]);
  161.           // Adicionar destinatários BCC (não incluídos no cabeçalho do e-mail)
  162.           for i := 0 to FBCC.Count - 1 do
  163.             vSMTP.MailTo(FBCC[i]);
  164.           vMimeMsg.EncodeMessage;  //glad
  165.           vSMTP.MailData(vMimeMsg.Lines);
  166.           vSMTP.Logout;
  167.         end;
  168.       except
  169.         on e:exception do
  170.           Result:=
  171.             'Erro "'+e.message+'": '+sLineBreak+
  172.             'Unidade: '+{$I %FILE%}+sLineBreak+
  173.             'Linha: '+{$INCLUDE %LINE%}+sLineBreak+
  174.             'Método: '+{$I %CURRENTROUTINE%};
  175.       end;
  176.     end;
  177.   finally
  178.     vSMTP.Free;
  179.     vMimeMsg.Free;
  180.   end;
  181. end;
  182.  
  183. end.
  184.  
« Last Edit: September 13, 2024, 04:13:11 pm by hamacker »

 

TinyPortal © 2005-2018