EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Problem sending e-mail

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#34913
Posted: 10/30/2015 09:46:45
by Eduardo Helminsky (Standard support level)
Joined: 08/20/2010
Posts: 102

I am using BlackBox to send e-mail from my applications. So far so good.

But just one application is having a problem. The application can Open the connection with the SMTP server, the login is made (at least without an error) and the problem seems to happen at the end of the process when I send "." (period) to close the connection. It takes a lot of time and then return without any error.

The message IS NOT sent.

What can I do to solve this problem ?
#34914
Posted: 10/30/2015 09:50:15
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

Is this problem reproducible with our sample? Is this problematic server accessible for us to reproduce the problem locally?
#34931
Posted: 11/03/2015 06:32:59
by Eduardo Helminsky (Standard support level)
Joined: 08/20/2010
Posts: 102

Sorry for the delay to response. Answering your questions: With the sample it works ok and yes the server is accessible to you. But after some tests I realize a weird thing. If I debug my code below, it works OK but if not, then sometimes it works and others don´t. Could you help me with this ? Just for note, with other customers that use other servers it is working perfectly.

Code
procedure TSMTP.SendLine(cCmd: String; bCRLF: Boolean = True);
begin
     if bCRLF then begin
        FSmtp.SendText(cCmd + #13#10);
     end else begin
        FSmtp.SendText(cCmd);
     end;
end;

procedure TSMTP.SendMsg;
begin
     FSmtp.Address := FHost;
     FSmtp.Port := FPort;
     FSmtp.Username := FUsername;
     if not SystemDevelop then begin
        if FUserNameA <> '' then begin
           FSmtp.Username := FUsernameA;
        end;
     end;
     FSmtp.Password := FPassword;

     if FCript = scSSL then begin
        // GMail
        FSmtp.UseSSL := True;
        FSmtp.SSLMode := smImplicit;
        FSmtp.OnCertificateValidate := CertificateValidateHandler;
     end else if FCript = scTLS then begin
        // Office365
        FSmtp.UseSSL := True;
        FSmtp.SSLMode := smExplicit;
        FSmtp.OnCertificateValidate := CertificateValidateHandler;
        {$IFDEF MAXIPLANT}
        FSmtp.Versions := [sbSSL3];
        {$ELSE}
        FSmtp.Versions := [sbTLS12];
        {$ENDIF}
     end else begin
        // Demais
        FSmtp.UseSSL := False;
        FSmtp.OnCertificateValidate := nil;
     end;

     FSmtp.SocketTimeout := 60000;
     FSmtp.DNS.QueryTimeout := 60000;
     FSmtp.DNS.TotalTimeout := 60000;

     FSmtp.SASLMechanism['DIGEST-MD5'] := False;
     FSmtp.SASLMechanism['CRAM-MD5'] := False;
     FSmtp.SASLMechanism['NTLM'] := False;

     for nI := 1 to 3 do begin
        try
           FSmtp.Open;
           break;
        except
           if nI = 3 then begin
              raise;
           end;
           Sleep(5000);
        end;
     end;

     try
        cAux := trim(FFromAddr);
        cAux := Copy(cAux,Pos('@',cAux)+1,Length(cAux));

        for nI := 1 to 3 do begin
           try
              FSmtp.Login(cAux);
              break;
           except
              if nI = 3 then begin
                 raise;
              end;
              Sleep(5000);
           end;
        end;

        // Origem
        FSmtp.SendCmd('MAIL FROM:<' + trim(FFromAddr) + '>',[250]);

        // Destino(s)
        for nI := 0 to FToAddr.Count-1 do begin
           cAux := trim(FToAddr[nI]);
           if cAux <> '' then begin
              FSmtp.SendCmd('RCPT TO:<' + cAux + '>',[250]);
           end;
        end;

        // Prepara para enviar a mensagem
        FSmtp.SendCmd('DATA',[354]);

        SendLine('MIME-version: 1.0');
        SendLine('X-Priority: 1');
        SendLine('X-Mailer: HPro Send Mail');
        SendLine('X-MimeOLE: Produced By HPro MimeOLE');
        SendLine('Date: ' + GetUTCDateTime(Now));
        if trim(FFromName) <> '' then begin
           SendLine('From: '+EncodeString(FFromName)+' <'+FFromAddr+'>');
        end else begin
           SendLine('From: <'+FFromAddr+'>');
        end;
        for nI := 0 to FToAddr.Count-1 do begin
           cAux := trim(FToAddr[nI]);
           if cAux <> '' then begin
              SendLine('To: <'+EncodeString(cAux)+'>');
           end;
        end;

        SendLine('Subject: '+EncodeString(FSubject));
        if FNotify then begin
           if trim(FFromName) <> '' then begin
              SendLine('Disposition-Notification-To: '+EncodeString(FFromName)+' <'+FFromAddr+'>');
           end else begin
              SendLine('Disposition-Notification-To: <'+FFromAddr+'>');
           end;
        end;

        SendLine('Content-type: multipart/mixed;');
        SendLine('              boundary="jeh18071968hej"');
        SendLine('');
        SendLine('This is a multipart message in MIME format.');
        SendLine('');
        SendLine('--jeh18071968hej');
        SendLine('Content-Type: text/plain; charset="iso-8859-1"');
        SendLine('Content-Transfer-Encoding: quoted-printable');
        SendLine('');
        SendText;
        SendLine('');
        for nI := 0 to FAttach.Count-1 do begin
           SendLine('--jeh18071968hej');
           SendAttachment(FAttach[nI]);
        end;
        SendLine('--jeh18071968hej--');
        SendLine('.');
     finally
        FSmtp.Close;
     end;
end;
#34932
Posted: 11/03/2015 06:37:41
by Eugene Mayevski (EldoS Corp.)

Could you please clarify, why you are sending all commands using SendLine?

TElSMTPClient is a high-level component which does everything for you.


Sincerely yours
Eugene Mayevski
#34933
Posted: 11/03/2015 07:01:02
by Eduardo Helminsky (Standard support level)
Joined: 08/20/2010
Posts: 102

Eugene

My SMTP component was so old and was designed without SecBlackBox components, then I just replace key pieces of the process because I don´t want to break things that is already working.

Did you see anything wrong or missing in my code ?
#34934
Posted: 11/03/2015 07:18:26
by Eugene Mayevski (EldoS Corp.)

First of all please try to send a message using the regular methods of the SMTP client. This will tell you if the delivery works, or there's a problem that needs resolution. Once this is done, we will know whether the problem is in your code or there's something else happening.


Sincerely yours
Eugene Mayevski
#34935
Posted: 11/03/2015 10:12:10
by Eduardo Helminsky (Standard support level)
Joined: 08/20/2010
Posts: 102

Eugene

Problem solved.

Using the method try/error I have found the server just need some time to process the end of message (with the caracter period). And I just put a sleep(1000) just before the SMTP call the close method. I am going to plan the change for use regular methods from SmtpClient.

Code
    
try
   ...
   SendLine('.');
finally
   Sleep(1000);    <---- Line added
   FSmtp.Close;
end;


Thanks for you help
#34936
Posted: 11/03/2015 11:46:30
by Eugene Mayevski (EldoS Corp.)

The actual problem comes from the fact that SMTP is a command-response protocol, and you seem to never read the responses. It would be better if you read the response after each command that you send with SendCmd() call and also after teh final dot that you send after the message. This way you would know, when the sending/receiving procedure is complete.


Sincerely yours
Eugene Mayevski
#34937
Posted: 11/03/2015 12:17:02
by Eduardo Helminsky (Standard support level)
Joined: 08/20/2010
Posts: 102

Eugene

Thanks again for the explanation. I have changed my code again with your suggestion and it works like a charm.

Code
try
   ...
   // SendLine('.'); // Replaced line
   FSmtp.SendCmd('.',[250]);   // It solves the problem with consistency
finally
   // Sleep(1000);   // Removed line
   FSmtp.Close;
end;


Always learning....
Also by EldoS: BizCrypto
Components for BizTalk® and SQL Server® Integration Services that let you securely store and transfer information in your business automation solutions.

Reply

Statistics

Topic viewed 4647 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!