wydadforever
10 years ago
hello,

I am currently developing an application in c# using easendmail, all is working fine , but i have one problem he send all email in the same time, i not want that , i want he send the first email than take 3 second and send the next email :

this my code :
List arRcpt = new List(logFile);
int nRcpt = arRcpt.Count;

SmtpMail[] arMail = new SmtpMail[nRcpt];
SmtpClient[] arSmtp = new SmtpClient[nRcpt];
SmtpClientAsyncResult[] arResult = new SmtpClientAsyncResult[nRcpt];
List list = loadSocks();
var oServer = new SmtpServer("");
for (int i = 0; i < nRcpt; i++)
{
oServer.SocksProxyServer = list[i%list.Count].IpAddress.ToString();
oServer.SocksProxyPort = list[i%list.Count].Port;
oServer.ProxyProtocol = SocksProxyProtocol.Socks5;
arMail[i] = new SmtpMail("TryIt");
arSmtp[i] = new SmtpClient();
SmtpMail oMail = arMail[i];
oMail.From = "";
oMail.Style = MailStyle.EASendMailStyle;
oMail.HeaderEncoding = HeaderEncodingType.EncodingBase64;

oMail.Subject = "";
oMail.TextBody = "";
oMail.AutoTextBody = false;
try
{
string fileHtml = Path.GetFullPath(Path.Combine(Application.StartupPath, "letter.html"));
oMail.ImportHtmlBody(fileHtml, ImportHtmlBodyOptions.NoOptions);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
SmtpClient oSmtp = arSmtp[i];
arResult[i] = oSmtp.BeginSendMail(oServer, oMail, null, null);
Console.WriteLine(String.Format("Start to send email to {0} ...",
arRcpt[i]));

}
// All emails were sent by BeginSendMail Method
// now get result by EndSendMail method
int nSent = 0;
while (nSent < nRcpt)
{
for (int i = 0; i < nRcpt; i++)
{
// this email has been sent
if (arResult[i] == null)
continue;


// wait for specified email ...
if (!arResult[i].AsyncWaitHandle.WaitOne(20, flase))
{
continue;
}

try
{
// this email is finished, using EndSendMail to get result
arSmtp[i].EndSendMail(arResult[i]);
Console.WriteLine(String.Format("Send email to {0} successfully",
arRcpt[i]));


}
catch (Exception ep)
{
Console.WriteLine(
String.Format("Failed to send email to {0} with error {1}: ",
arRcpt[i], ep.Message));
Console.ReadLine();
}

// Set this email result to null, then it won't be processed again
arResult[i] = null;
nSent++;
}
}
}
if i send all the email in the same time he stop send email, and give me the error too much connections 421

what i should do ???

tnks
ivan
  • ivan
  • 100% (Exalted)
  • Administration
10 years ago
Hi, it seems that remote SMTP server limit concurrent connections number, so I suggest that you only use one SmtpClient instead of SmtpClient array.

please have a look at the following codes:


List<string> arRcpt = new List<string>(logFile);
int nRcpt = arRcpt.Count;

SmtpClient oSmtp = new SmtpClient();
SmtpClientAsyncResult oResult = null;
List<ServerSocks> list = loadSocks();
var oServer = new SmtpServer("");
for (int i = 0; i < nRcpt; i++)
{
    oServer.SocksProxyServer = list[i % list.Count].IpAddress.ToString();
    oServer.SocksProxyPort = list[i % list.Count].Port;
    oServer.ProxyProtocol = SocksProxyProtocol.Socks5;

    SmtpMail oMail = new SmtpMail("TryIt");
    oMail.From = "";
    oMail.Style = MailStyle.EASendMailStyle;
    oMail.HeaderEncoding = HeaderEncodingType.EncodingBase64;

    oMail.Subject = "";
    oMail.TextBody = "";
    oMail.AutoTextBody = false;
    try
    {
        string fileHtml = Path.GetFullPath(Path.Combine(Application.StartupPath, "letter.html"));
        oMail.ImportHtmlBody(fileHtml, ImportHtmlBodyOptions.NoOptions);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }

    oResult = oSmtp.BeginSendMail(oServer, oMail, null, null);
    Console.WriteLine(String.Format("Start to send email to {0} ...",
    arRcpt[i]));
    // wait for specified email ...
    while (!oResult.AsyncWaitHandle.WaitOne(20, false))
    {
        continue;
    }

    try
    {
        // this email is finished, using EndSendMail to get result
        oSmtp.EndSendMail(oResult);
        Console.WriteLine(String.Format("Send email to {0} successfully",
        arRcpt[i]));
    }
    catch (Exception ep)
    {
        Console.WriteLine(
        String.Format("Failed to send email to {0} with error {1}: ",
        arRcpt[i], ep.Message));
        Console.ReadLine();
    }

    System.Threading.Thread.Sleep(1000 * 3); // sleep 3 seconds
}

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.