Recent

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

ramutau

  • New Member
  • *
  • Posts: 40
[SOLVED] Faster Email
« on: June 09, 2017, 04:42:38 pm »
Hi All,

I'm using Lazarus 1.6 r51630 FPC 3.0.0 i386-win32-win32/win64

I'm sending emails and the average speed is between 4 and 7 seconds.

No attachments, just a std email with a subjet and a body, one recipient.

I have tried Synapse, XMailer, INDY, as well as using external apps like SendEmail using TProcess.

Does anyone have an idea of how I can speed this up?

Users are complaining about waiting for 5 seconds while the email sends.

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

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Faster Email
« Reply #1 on: June 09, 2017, 05:09:06 pm »
The speed of sending an email is not based on code it has to do with the network. You essentially waiting for the server to inform you that it received the message correctly. everything plays its role including the distance from your ISP how many hopes are in between you and your mail server even if there was a power failure between you and your server and a new root needs to be found. Do the smart thing send the email asynchronously use a thread, inform the user only if the server reports a problem.
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

Handoko

  • Hero Member
  • *****
  • Posts: 5425
  • My goal: build my own game engine using Lazarus
Re: Faster Email
« Reply #2 on: June 09, 2017, 05:27:58 pm »
I use Yahoo mail. Usually the email can be received by the recipient just in seconds. But it ever take 2 days (if I remember correctly) to be received. Server issue I guess.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Faster Email
« Reply #3 on: June 09, 2017, 05:31:46 pm »
I use Yahoo mail. Usually the email can be received by the recipient just in seconds. But it ever take 2 days (if I remember correctly) to be received. Server issue I guess.
don't confuse your email server with the recipients email server those are two different time frames.
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

Handoko

  • Hero Member
  • *****
  • Posts: 5425
  • My goal: build my own game engine using Lazarus
Re: Faster Email
« Reply #4 on: June 09, 2017, 06:44:58 pm »
Oh, my mistake. I didn't read OP post thoroughly.

ramutau

  • New Member
  • *
  • Posts: 40
Re: Faster Email
« Reply #5 on: June 09, 2017, 07:23:25 pm »
Do the smart thing send the email asynchronously use a thread, inform the user only if the server reports a problem.

Thank you. Please will you point me to where I can read up on this. I have no idea how to "send the email asynchronously use a thread"

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: Faster Email
« Reply #6 on: June 09, 2017, 07:54:15 pm »
Do the smart thing send the email asynchronously use a thread, inform the user only if the server reports a problem.

Thank you. Please will you point me to where I can read up on this. I have no idea how to "send the email asynchronously use a thread"
Taazz that is nonsense. Every code in even a scripting language will outperform the connection speed. I have a speed of about 100Gb (on-line, and somewhat stable). The code can process at least a 100 times more.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Faster Email
« Reply #7 on: June 09, 2017, 08:26:36 pm »
Thank you. Please will you point me to where I can read up on this. I have no idea how to "send the email asynchronously use a thread"
Taazz that is nonsense. Every code in even a scripting language will outperform the connection speed. I have a speed of about 100Gb (on-line, and somewhat stable). The code can process at least a 100 times more.
So far you agree with me. I'll wait for you to point me to the nonsense part.

Thank you. Please will you point me to where I can read up on this. I have no idea how to "send the email asynchronously use a thread"
Asynchronous sockets is a windows concept and not a very popular one. If your application is for windows only then you could try using Internet Component Suite by Francois Piette which a suite much like indy but it uses non blocking sockets instead of blocking. Or you could avoid all this, write a TThread class that, by its nature, executes in parallel with the rest of your application and send your email through it.
Be warned multi threading programming has a steep learning curve, having said that you have to remember one thing when you start multi threading,
do not access variables/object outside the thread class. If there is something you might need pass it to the thread before it starts executing if you want to inform your main application for the progress do it after it has finished executing.

There are a couple of libraries that might make the task easier like mtprocs, PasMP(fpc, lazarus)  AsyncCalls, OmniThreadLibrary (delphi) but the above rule is universal.
In any case a tutorial on simple threading is to big for a forum post. Search the wiki it has the basics documented including basic information for the mtproc library
« Last Edit: June 09, 2017, 08:29:43 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

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Faster Email
« Reply #8 on: June 09, 2017, 09:28:09 pm »
Yeah, I think Thaddy got confused there  :P

Besides going to a thread (which I would also suggest) I'm really wondering why a simple mail would take 5-7 seconds. Testing with two different providers here I get nowhere close to that. So going to a thread would alleviate that but searching for the real cause could also help.

sam707

  • Guest
Re: Faster Email
« Reply #9 on: June 09, 2017, 11:39:59 pm »
Thank you. Please will you point me to where I can read up on this. I have no idea how to "send the email asynchronously use a thread"
Taazz that is nonsense. Every code in even a scripting language will outperform the connection speed. I have a speed of about 100Gb (on-line, and somewhat stable). The code can process at least a 100 times more.
So far you agree with me. I'll wait for you to point me to the nonsense part.

Thank you. Please will you point me to where I can read up on this. I have no idea how to "send the email asynchronously use a thread"
Asynchronous sockets is a windows concept and not a very popular one. If your application is for windows only then you could try using Internet Component Suite by Francois Piette which a suite much like indy but it uses non blocking sockets instead of blocking. Or you could avoid all this, write a TThread class that, by its nature, executes in parallel with the rest of your application and send your email through it.
Be warned multi threading programming has a steep learning curve, having said that you have to remember one thing when you start multi threading,
do not access variables/object outside the thread class. If there is something you might need pass it to the thread before it starts executing if you want to inform your main application for the progress do it after it has finished executing.

There are a couple of libraries that might make the task easier like mtprocs, PasMP(fpc, lazarus)  AsyncCalls, OmniThreadLibrary (delphi) but the above rule is universal.
In any case a tutorial on simple threading is to big for a forum post. Search the wiki it has the basics documented including basic information for the mtproc library

not sure that ASYNC sockets is a windows concept
"It is possible to do nonblocking I/O on sockets by setting the
       O_NONBLOCK flag on a socket file descriptor using fcntl(2).  Then all
       operations that would block will (usually) return with EAGAIN
       (operation should be retried later); connect(2) will return
       EINPROGRESS error.  The user can then wait for various events via
       poll(2) or select(2)."
http://man7.org/linux/man-pages/man7/socket.7.html

what is certain, is that bad workers always complain about bad tools (concepts) ;)

I clearly HEAVILY use the lNet components package, on 3 PC (1 windows, 2 linux) and the components are well known to be non-blocking (TLCLEventer class) for sure, they work like a charm on each OS
« Last Edit: June 09, 2017, 11:42:16 pm by sam707 »

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Faster Email
« Reply #10 on: June 10, 2017, 04:20:35 am »
Users are complaining about waiting for 5 seconds while the email sends.
I always hear Windows users complaining about the connection time.
And the answer is always the same: configure your program to be disregarded by the antivirus and the firewall.
Works fine!
PS: I use TSMTPSend (synapse).

ramutau

  • New Member
  • *
  • Posts: 40
Re: Faster Email
« Reply #11 on: June 10, 2017, 02:08:06 pm »
Thanks for all the responses. Appreciated.

This is the piece of my code:

         Mail.Smtp.UserName := Gmail_Address_VAR;
         Mail.Smtp.Password := Gmail_Password_VAR;
         Mail.Smtp.Host := 'smtp.gmail.com';
         Mail.Smtp.Port := '465';
         Mail.Smtp.SSL := True;
         Mail.Smtp.TLS := True;
         Mail.Send; 

It takes 5 to 7 seconds just on the Mail.Send statement.

I have excluded the app from the Firewall and AV.

I don't know anything about asynchronous multithreading, but I'm going to learn.

Noob question: Am I just going to run the one statement "Mail.Send" in another thread?

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Faster Email
« Reply #12 on: June 10, 2017, 02:14:33 pm »
First... don't just exclude your app from the firewall and AV but disable the firewall completely and uninstall the AV.

Even after disabling AV, it could still cause problems. Uninstalling it completely is the only way to discard it.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Faster Email
« Reply #13 on: June 10, 2017, 02:27:12 pm »
@rvk:
... And say hello to the next ransomware ...
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Faster Email
« Reply #14 on: June 10, 2017, 02:27:39 pm »
Quote
I always hear Windows users complaining about the connection time.
And the answer is always the same: configure your program to be disregarded by the antivirus and the firewall.
Works fine!

A firewall shouldn't be the problem, but AV-Software is big bullshit ...
Of course not for all people... not for the salesmen and company owners.   :)
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018