Forum > General

Using synapse to send email

(1/1)

hamacker:
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:

--- Quote from: hamacker on September 12, 2024, 11:52:21 pm ---It has three methods, all for attaching files that no longer exist.

--- End quote ---
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.

hamacker:
Excelent tips.
I think that I got it! I sent my first email using synapse.
My code now for help others:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit utils_email; interface { Sample:with TUtilsEMailSender.Create dobegin  Host := 'localhost';  Port := 587;  Username := 'me@domain.com';  Password := 'secret';  From := 'me@domain.com';  Recipients.Add('johdoe@domain.com');  CC.Text:='cc1@domain.com'+sLineBreak+'cc2@domain.com';  BCC.Text:='bcc1@domain.com'+sLineBreak+'bcc2@domain.com';  Subject := edtSubject.Text;  Body := edtBody.Text;  //Attachments.Text := 'attach1.ods' + sLineBreak + 'attach2.html';  Attachments.Add('attach1.ods');  Attachments.Add('attach2.html');  Result := Send;  if Result = '' then    ShowMessage('SUCESS')  else    ShowMessage('FAIL');  Free;end;}  uses  Classes, SysUtils; type  TUtilsEMailSender = class  private    FHost: string;    FPort: Integer;    FUsername: string;    FPassword: string;    FFrom: string;    FRecipients: TStrings;    FCC: TStrings;    FBCC: TStrings;    FSubject: string;    FBody: string;    FAttachments: TStringList;   public    constructor Create;    destructor Destroy; override;     function Send: String;     property Host: string read FHost write FHost;    property Port: Integer read FPort write FPort;    property Username: string read FUsername write FUsername;    property Password: string read FPassword write FPassword;    property From: string read FFrom write FFrom;    property Recipients: TStrings read FRecipients write FRecipients;    property CC: TStrings read FCC write FCC;    property BCC: TStrings read FBCC write FBCC;    property Subject: string read FSubject write FSubject;    property Body: string read FBody write FBody;    property Attachments: TStringList read FAttachments write FAttachments;  end; implementation uses  smtpsend, mimemess, mimepart; { TUtilsEMailSender } constructor TUtilsEMailSender.Create;begin  FRecipients := TStringList.Create;  FCC := TStringList.Create;  FBCC := TStringList.Create;  FAttachments := TStringList.Create;end; destructor TUtilsEMailSender.Destroy;begin  FRecipients.Free;  FCC.Free;  FBCC.Free;  FAttachments.Free;  inherited;end; function TUtilsEMailSender.Send: String;var  vSMTP: TSMTPSend;  vMimeMsg: TMimeMess;  vMimePart: TMimePart;  vTextPart: TStrings;  vFileStream: TFileStream;  i: Integer;begin  Result := '';  vSMTP := TSMTPSend.Create;  vMimeMsg := TMimeMess.Create;  if (Pos('@', FFrom)<=0) then    Result:='Você não informou o remetente.';  if (Result='') and (Pos('@', FRecipients.Text)<=0)  then    Result:='Você não informou o destinatario.';  if (Result='') and (Trim(FSubject)='')  then    Result:='Você não informou o assunto.';  if (Result='') and (Trim(FBody)='')  then    Result:='Você não informou o conteudo da mensagem.';  try    if Result='' then    begin      try        // Configuração do servidor SMTP        vSMTP.TargetHost := FHost;        vSMTP.TargetPort := IntToStr(FPort);        vSMTP.Username := FUsername;        vSMTP.Password := FPassword;         // Configuração do e-mail        vMimeMsg.Header.From := FFrom;        for i := 0 to FRecipients.Count - 1 do          vMimeMsg.Header.ToList.Add(FRecipients[i]);        for i := 0 to FCC.Count - 1 do          vMimeMsg.Header.CCList.Add(FCC[i]);        vMimeMsg.Header.Subject := FSubject;         // Criação da parte mista (para separar corpo e anexos)        vMimePart := vMimeMsg.AddPartMultipart('mixed', nil);         // Adicionar corpo do e-mail como texto        vTextPart := TStringList.Create;        try          vTextPart.Text := FBody;          vMimeMsg.AddPartText(vTextPart, vMimePart);        finally          vTextPart.Free;        end;         // Adicionar anexos        for i := 0 to FAttachments.Count - 1 do        begin          vFileStream := TFileStream.Create(FAttachments[i], fmOpenRead or fmShareDenyWrite);          try            vMimeMsg.AddPartBinary(vFileStream, ExtractFileName(FAttachments[i]), vMimePart);          finally            vFileStream.Free;          end;        end;         // Conectar ao servidor SMTP e enviar o e-mail        if vSMTP.Login then        begin          vSMTP.MailFrom(FFrom, Length(FBody));          for i := 0 to FRecipients.Count - 1 do            vSMTP.MailTo(FRecipients[i]);          for i := 0 to FCC.Count - 1 do            vSMTP.MailTo(FCC[i]);          // Adicionar destinatários BCC (não incluídos no cabeçalho do e-mail)          for i := 0 to FBCC.Count - 1 do            vSMTP.MailTo(FBCC[i]);          vMimeMsg.EncodeMessage;  //glad          vSMTP.MailData(vMimeMsg.Lines);          vSMTP.Logout;        end;      except        on e:exception do          Result:=            'Erro "'+e.message+'": '+sLineBreak+            'Unidade: '+{$I %FILE%}+sLineBreak+            'Linha: '+{$INCLUDE %LINE%}+sLineBreak+            'Método: '+{$I %CURRENTROUTINE%};      end;    end;  finally    vSMTP.Free;    vMimeMsg.Free;  end;end; end. 

Navigation

[0] Message Index

Go to full version