Page 1 of 1

TScSMTPClient - Attachment File in E-mail

Posted: Wed 16 Dec 2020 07:43
by klopotsky
For the send E-mail, i am use TScSMTPClient.
Configuring the component:

Code: Select all

procedure TdmMain.ScSMTPClientBeforeConnect(Sender: TObject);
begin
  with ScSMTPClient do begin
    HostName := '... il-01-sh.hoster.by';
    Port := 465;
    Username := '... [email protected]';
    Password := '... &i4P1UO';
    TLSMode := tmImplicitTLS;
    AuthenticationType := satDefault;
  end;
end;
The component TScSMTPClient has a method:
Send(const From: string; const Recipients: string; const Subject: string; const Body: string);

The E-mail is sent well:

Code: Select all

with ScSMTPClient do begin
  Connect;
  if Authenticate then Send('[email protected]', '[email protected]', 'My Subject', 'My Body');
end;
How can I add a file to my E-mail?
How to work with 'TScMailMessage'?

Re: TScSMTPClient - Attachment File in E-mail

Posted: Fri 18 Dec 2020 16:17
by ViktorV
To solve the issue, you can use the following code:

Code: Select all

  ScSMTPClient1.Connect;
  if ScSMTPClient1.Authenticate then begin
    Message := TScMailMessage.Create;
    try
      Message.From.AsString := '[email protected]';
      Message.ToAddress.AsString := '[email protected]';
      Message.Subject := 'My Subject with picture';
      Message.Body.Text := 'My Body';
      Attachment := TScAttachment.Create(Message.Attachments, 'c:\pic.jpg');
      ScSMTPClient1.Send(Message);
    finally
      Message.Free;
    end;
  end;
Note that to run this code, you should add the ScMailMessage, ScSMTPUtils units to the USES clause of your unit.

Re: TScSMTPClient - Attachment File in E-mail

Posted: Fri 15 Jan 2021 15:04
by tabu
I've not found any documentation about TScMailMessage, so I ask here.

Sending Messages over 73 Charakters causes line breaks at positions I don't want.

Reason is the Quoted-printable-Encoding.

My code is

Code: Select all

procedure TBafWebModule.EmailSend;
var
  LMess: TScMailMessage;
  LAttach: TScAttachment;
  i, LCount: integer;
begin
  if not FMailClient.Active then
    FMailClient.Connect;
  if FMailClient.Authenticate then begin
    LMess := TScMailMessage.Create;
    try
      LMess.Encoding := meMIME;
      LMess.From.AsString := FindParamStringReplaced('from', '');
      LMess.ReplyTo.AsString := FindParamStringReplaced('reply', '');
      LMess.ToAddress.AsString := FindParamStringReplaced('to', '');
      LMess.CC.AsString := FindParamStringReplaced('cc', '');
      LMess.BCC.AsString := FindParamStringReplaced('bcc', '');
      LMess.Subject := FindParamStringReplaced('subject', '');
      LMess.Body.Text := FindParamStringReplaced('text', '');
      LMess.BodyEncoding := 'multipart';
      LCount := FindParamIntegerReplaced('cnt', 0);
      for i := 1 to LCount do begin
        LAttach := TScAttachment.Create(LMess.Attachments,
          FindParamStringReplaced('fn' + IntToStr(i), ''));
      end;
      FMailClient.Send(LMess);
    finally
      LMess.Free;
    end;
  end
  else
    FInter.DoLog('E', '#email_send, authenification failed');
end;
A workaround is to remove the last lines in TScCoderQuotedPrintable.InternalEncode. (I don't think, the problem is in this routine, but in using this encoding in this case. But it solves the problem in my project...)

Code: Select all

procedure TScCoderQuotedPrintable.InternalEncode(const Data: TBytes; Offset, Count: integer;
  var OutBuf: TBytes; OutOffset: integer; out OutCount: integer);
var
  EndPos, LineLen: integer;
  i: integer;
begin
  OutCount := Count * 3 + ((Count div 73) + 2) * 3;
  if Length(OutBuf) < OutOffset + OutCount then
    SetLength(OutBuf, OutOffset + OutCount);

  OutCount := 0;
  LineLen := 0;
  EndPos := Offset + Count - 1;
  for i := Offset to EndPos do begin
    if (Data[i] >= 33) and (Data[i] <= 126) and (Data[i] <> 61{=}) then begin
      OutBuf[OutOffset] := Data[i];
      Inc(OutOffset);
      Inc(OutCount);
      Inc(LineLen);
    end
    else
    if ((Data[i] = 9) or (Data[i] = 32)) and (i < EndPos) then begin
      OutBuf[OutOffset] := Data[i];
      Inc(OutOffset);
      Inc(OutCount);
      Inc(LineLen);
    end
    else begin
      OutBuf[OutOffset] := 61{=};
      OutBuf[OutOffset + 1] := Ord(TwoHexUpperLookup[Data[i]][1]);
      OutBuf[OutOffset + 2] := Ord(TwoHexUpperLookup[Data[i]][2]);
      Inc(OutOffset, 3);
      Inc(OutCount, 3);
      Inc(LineLen, 3);
    end;

//    if (LineLen >= 73) and (i < EndPos) then begin
//      OutBuf[OutOffset] := 61{=};
//      OutBuf[OutOffset + 1] := 13;
//      OutBuf[OutOffset + 2] := 10;
//      Inc(OutOffset, 3);
//      Inc(OutCount, 3);
//      LineLen := 0;
//    end;

  end;
end;
Documentation would be nice...

Re: TScSMTPClient - Attachment File in E-mail

Posted: Mon 18 Jan 2021 17:20
by ViktorV
The description of the TScSMTPClient class will be added to the documentation in the next build of SecureBridge.

Unfortunately, we could not reproduce the issue.
In order for us to be able to give you a detailed answer, please compose a small sample demonstrating the described behavior and send it to us through the contact form https://devart.com/company/contactform.html including the scripts for creating database objects.

Re: TScSMTPClient - Attachment File in E-mail

Posted: Wed 10 Mar 2021 09:20
by Thom995
I am facing the same problem.

Did you find a solution to this ?

This is my source code:

ScSMTPClient.Connect;
if ScSMTPClient.Authenticate then
begin
LMess := TScMailMessage.Create;
try
LMess.From.AsString := 'from@mail';
LMess.ToAddress.AsString := 'To@mail';
LMess.Subject := 'Test';
LMess.Body.Text := 'Example Text 1' + #13 + 'Example 2' + #13 +'Umlauttest ÄÜÖ'+ #13 + #13 + 'This is a long text';
LMess.Encoding := meDefault;
LAttach := TScAttachment.Create(LMess.Attachments, 'C:\test.png');
ScSMTPClient.Send(LMess);
finally
LMess.Free;
end;
end;

Reciving the E-Mail, I get an additional line break:
W-Mail:
"This i" ==> line brake ==> "s a long text".

Re: TScSMTPClient - Attachment File in E-mail

Posted: Thu 11 Mar 2021 13:11
by ViktorV
We've assigned a high priority status to this issue and will include a fix in the next version of Securebridge, which is to be released this month.

Re: TScSMTPClient - Attachment File in E-mail

Posted: Fri 26 Mar 2021 12:41
by ViktorV
Thank you for the information. We have reproduced and fixed the issue. This fix will be included in the next build of SecureBridge, which we're planning to release next week.