Recent

Author Topic: [SOLVED] Faster Email  (Read 16799 times)

ramutau

  • New Member
  • *
  • Posts: 40
Re: Faster Email
« Reply #30 on: June 10, 2017, 09:07:02 pm »
Thanks.

Apologies if I wasn't clear in what the problem is.

That is the way all email clients work, take a look on mozila thunderbird for example, it does not block you from working but the "mail window" remains open for a few seconds after you press the send button. Why don't you install thunderbird and send an email with it for testing see how long it takes thunderbird to send it and compare that with your application.

It takes 5 seconds in Thunderbitd as well on my PC/Connection. BUT, in Thunderbird I can carry on doing other things, as was pointed out.

In my project I can't. I have to wait for the email to send.

I attach a project with your code, as well as a screenshot.

As you can see, I have added a button to the form that does nothing but increment a counter an display that on a label.

As soon as I click the 'SEND' button, I cannot click on the ADD button anymore until the send completes or fails. So the app hasn't frozen, it's just waiting.

Thanks for your time, I appreciate it.

« Last Edit: June 11, 2017, 01:29:16 pm by ramutau »

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Faster Email
« Reply #31 on: June 10, 2017, 09:12:32 pm »
If the sendtoex() from my example also takes 5-7 seconds and Thunderbird does too there might be something in your computer slowing it down. (My example takes 1,5 seconds for me)

(I take it the example does really send the mail and returns successful !)

Anyway... You can try the next example I have with using a thread.

ramutau

  • New Member
  • *
  • Posts: 40
Re: Faster Email
« Reply #32 on: June 10, 2017, 09:14:32 pm »
Thanks rvk. I will do that.

Am I correct in understanding (apart from the speed issue) that it is normal for the system to wait until the email has finished sending?

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Faster Email
« Reply #33 on: June 10, 2017, 09:17:16 pm »
Am I correct in understanding (apart from the speed issue) that it is normal for the system to wait until the email has finished sending?
Yes, because it also needs to return the result of that sending. Otherwise you wouldn't know the mail was send successful.

(And the smtp library of synapse is "blocking")

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Faster Email
« Reply #34 on: June 10, 2017, 10:00:06 pm »
Thanks.

Apologies if I wasn't clear in what the problem is.

That is the way all email clients work, take a look on mozila thunderbird for example, it does not block you from working but the "mail window" remains open for a few seconds after you press the send button. Why don't you install thunderbird and send an email with it for testing see how long it takes thunderbird to send it and compare that with your application.

It takes 5 seconds in Thunderbitd as well on my PC/Connection. BUT, in Thunderbird I can carry on doing other things, as was pointed out.
which makes it a system or network problem not a code problem.

In my project I can't. I have to wait for the email to send.
To solve this you need to use threads or non blocking sockets. I would recommend threads obviously but its your decision to make, both solutions require time and can't be applied pronto as you have put it.
I attach a project with your code, as well as a screenshot.
Got it I'll take a closer look when I find a bit of time but I doubt I'll see anything useful. I might find some problems in my network/system if I dig deep enough but I doubt that would be of any use to you.

As you can see, I have added a button to the form that does nothing but increment a counter an display that on a label.

As soon as I click the 'SEND' button, I cannot click on the ADD button anymore until the send completes or fails. So the app hasn't frozen, it's just waiting.
That's what I mend when I said "freeze while sending" the application stops responding for what ever reason for a period of time.

there are a couple of questions you need to answer.
1) Are the changes you are going to make helpful to your end users or not? If for example your end users use a different provider than google that has different requirements, would the changes in your code be as fast there or there is a chance that they will loose speed instead?
2) Are you responsible for their network and response times?
3) Do you plan on providing network services on top of the program?
4) Do you want to have to explain to each end user the reason why your application waits?
5) Do you want to have to prove that its not your application but their systems?
6) Who is going to pay for all that time spend? The end users or you?

overall its your decision to make I can only tell you what I would do in your position.
Am I correct in understanding (apart from the speed issue) that it is normal for the system to wait until the email has finished sending?
depends what do you mean by system? do you mean the OS, application or send process? Anyway the smtp protocol is well defined and it does not have any optional steps which means that the server is obliged to send a response that the communication ended correctly. It does not, how ever, define if you have to wait for the response or inform the end user about its status.

Hope that clears up a couple of things. fill free to ask more questions if it does not.
« Last Edit: June 10, 2017, 10:03:30 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ramutau

  • New Member
  • *
  • Posts: 40
Re: Faster Email
« Reply #35 on: June 10, 2017, 10:08:38 pm »
If the sendtoex() from my example also takes 5-7 seconds and Thunderbird does too there might be something in your computer slowing it down. (My example takes 1,5 seconds for me)

(I take it the example does really send the mail and returns successful !)

Anyway... You can try the next example I have with using a thread.

YAY! Progress. I can now click on 'SEND', and then carry on clicking on the other button, and the counter works etc. The email doesn't send, and I get an error back after 4 or 5 seconds, but at least it is doing this in the background, and I'm not 'frozen out' while this happening!

Now I just have to figure out why it returns an error and doesn't send, but this is exactly what I was trying to achieve.

Thank you so much. I've learned a lot tonight, and need to study some more, but I think I am on the right track :-)

(If I can't find the error I'm going to come knocking on your door again :-)

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Faster Email
« Reply #36 on: June 10, 2017, 10:17:51 pm »
Now I just have to figure out why it returns an error and doesn't send, but this is exactly what I was trying to achieve.
Ok, so the thread works.

Now for the error.
I have a few questions.

*) I already asked if the sendtoex() worked without the thread? Did it return "error" or "successful" ?

The possible reasons for your error are:
*) Have you added ssl_openssl to your uses of your unit?

*) Have you changed the smtpsend.pas from synapse that  SMTP.FullSSL := True; is uncommented?

*) Do you have the openssl dll's in your .exe project directory ?

When SSL doesn't work you get a delay of about 5-7 seconds before the sendtoex() procedure returns. So that could be the cause. When you fix these things the sending of the mail should take about 1,5 seconds.

ramutau

  • New Member
  • *
  • Posts: 40
Re: Faster Email
« Reply #37 on: June 10, 2017, 10:49:06 pm »
* sendtoex() returns "Error"

* My uses clause:

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, Grids, ValEdit, smtpsend, ssl_openssl;

* Uncommented:

// if you need support for TSL/SSL tunnel, uncomment next lines:
     SMTP.FullSSL := True;     

* I didn't have the DLLs in the .exe project directory, but have now put them there:

libeay32.dll
ssleay32.dll

However, I still get the ERROR message after 7 or 8 seconds.

I must add that this is all new to me, and I have copied and pasted your code into my project where I thought it should go, without really knowing what I'm doing yet, LOL! So it could just be me. I shall spend the night studying up on threading. :-)

JUST in case you have some time, and at the risk of embarrasing myself, I have attached my little project :-)

THANK YOU! :-)

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Faster Email
« Reply #38 on: June 10, 2017, 11:02:01 pm »
Those are some old openssl dll :D
You might want to get some newer ones here: https://indy.fulgan.com/SSL/
Make sure to take the 32bit ones if you're compiling in 32 bit
and the 64bit ones if you're compiling in 64 bit in Lazarus.

Second, I'm not sure what the XMailerPkg is but I use the base Synapse library. But I guess the XMailerPkg contains those files too.

Last... you code works for me but only with my own username and password. I see you didn't use the constant Username and Password in the sendtoex() call (but they are the same). But when I use yours I also get an error. When I use mine I get success.

ramutau

  • New Member
  • *
  • Posts: 40
Re: Faster Email
« Reply #39 on: June 10, 2017, 11:04:05 pm »
Thanks rvk. I really appreciate your time and help. I'll check the DLLs.

You are my HERO! :-)

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Faster Email
« Reply #40 on: June 10, 2017, 11:06:25 pm »
And the execute needs to be:
Code: Pascal  [Select][+][-]
  1. procedure TMyMailThread.Execute;
  2. begin
  3.   if SendToEx(FMailFrom, FMailTo, FSubject, 'smtp.gmail.com:465', FMessage, Username, Password) then
  4.     Synchronize(@ShowSuccess)
  5.   else
  6.     Synchronize(@ShowError);
  7. end;
That way you can pass MailFrom and MailTo and Subject in the creation of the thread.

Like this:
Code: Pascal  [Select][+][-]
  1. MyMailThread := TMyMailThread.Create('From@mail.com', 'to@mail.com', 'Subject', Message);

Otherwise you don't do anything with the FFromMail etc in the thread.

And make sure the Username and Password are correct.
(and use the newest openssl dlls)

PS. The FromMail will likely always be the same as UserName in gmail (unless you have authenticated second e-mail addresses configured. Otherwise gmail always sends with the same e-mail address as the useraccount.
« Last Edit: June 10, 2017, 11:10:07 pm by rvk »

ramutau

  • New Member
  • *
  • Posts: 40
Re: Faster Email
« Reply #41 on: June 11, 2017, 01:24:07 pm »
Thanx so much rvk. All working like a charm! :D

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: [SOLVED] Faster Email
« Reply #42 on: June 11, 2017, 04:01:02 pm »
How long does sending the mail now take for you (before you get confirmation of success)?

If it's also 1,5 seconds, I wonder if your users in your opening post could really send mail. Or did the mail also not send successful for them? (Which would be a vital piece of info you should have shared :D)

ramutau

  • New Member
  • *
  • Posts: 40
Re: [SOLVED] Faster Email
« Reply #43 on: June 11, 2017, 06:27:55 pm »
Strangely, the first email takes about 5 seconds before I get the result back, then subsequent emails take about 2 to 3 seconds, all successful.

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: [SOLVED] Faster Email
« Reply #44 on: June 11, 2017, 06:50:09 pm »
Strange. But at least it's working (again) and now you know about threads :D

 

TinyPortal © 2005-2018