Sending 2 email alerts sequentially

Created at 18 May 2022, 15:42
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CO

couscousmckoy

Joined 07.02.2018

Sending 2 email alerts sequentially
18 May 2022, 15:42


Hi

I have a cbot which fires off an email to myself (EmailAddress1) when a certain condition is met.  This works fine.  I have a friend who I wanted to send the same email to so I added a second email variable (EmailAddress2).

Unfortunately when the code now runs I get the following error message in the log (and the email only gets sent to one address - interestingly not always the first address that it encounters in the code?!):

Failed to send email "cTrader 5 Wick Alert". System.Net.Mail.SmtpException: Syntax error, command unrecognized. The server response was: 4.3.2 Concurrent connections limit exceeded. Visit https://aka.ms/concurrent_sending for more information. [Hostname=DB9P193MB1966.EURP193.PROD.OUTLOOK.COM]    at System.Net.Mail.DataStopCommand.CheckResponse(SmtpStatusCode statusCode, String serverResponse)    at System.Net.Mail.DataStopCommand.Send(SmtpConnection conn)    at System.Net.ClosableStream.Close()    at System.Net.Mail.SmtpClient.Complete(Exception exception, IAsyncResult result)

Any ideas why?  Does SMTP restrict to 1 email at a time? I'm only trying to send 2 emails!!

Thanks in advance for any help / guidance.
Marcus

This is the code snippet:

try
            {
                Notifications.SendEmail(EmailAddress1, EmailAddress1, "cTrader 5 Wick Alert", emailmsg);
            } catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Invalid email address", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (EmailAddress2 != "")
            {
                try
                {
                    Notifications.SendEmail(EmailAddress1, EmailAddress2, "cTrader 5 Wick Alert", emailmsg);
                } catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Invalid email address", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

 


@couscousmckoy
Replies

amusleh
19 May 2022, 09:34

Hi,

It depends on your email server that how many emails it allows you to send on a unit of time (seconds/milliseconds).

To avoid this issue you can place a delay after sending the first email, use Thread.Sleep, example:

            try
            {
                Notifications.SendEmail(EmailAddress1, EmailAddress1, "cTrader 5 Wick Alert", emailmsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Invalid email address", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (EmailAddress2 != "")
            {
                // 2 seconds delay
                Thread.Sleep(2000);
                // Call refresh data is required after suspending the main thread
                RefreshData();
                
                try
                {
                    Notifications.SendEmail(EmailAddress1, EmailAddress2, "cTrader 5 Wick Alert", emailmsg);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Invalid email address", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

 


@amusleh

couscousmckoy
20 May 2022, 13:57

RE:

Thank you Amusleh. I appreciate you taking the time to reply. I actually decided just to forward the emails on to the second address from Outlook with a rule. But I can see your solution would be successful too.

Marcus

 

amusleh said:

Hi,

It depends on your email server that how many emails it allows you to send on a unit of time (seconds/milliseconds).

To avoid this issue you can place a delay after sending the first email, use Thread.Sleep, example:

            try
            {
                Notifications.SendEmail(EmailAddress1, EmailAddress1, "cTrader 5 Wick Alert", emailmsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Invalid email address", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (EmailAddress2 != "")
            {
                // 2 seconds delay
                Thread.Sleep(2000);
                // Call refresh data is required after suspending the main thread
                RefreshData();
                
                try
                {
                    Notifications.SendEmail(EmailAddress1, EmailAddress2, "cTrader 5 Wick Alert", emailmsg);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Invalid email address", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

 

 


@couscousmckoy