views:

207

answers:

3

here is my code

for(int i = 0; i < number ; i++)
{
  MailAddress to = new MailAddress(iMail.to);
  MailAddress from = new MailAddress(iMail.from, iMail.displayName);
  string body = iMail.body;
  string subject = iMail.sub;
  oMail = new MailMessage(from, to);
  oMail.Subject = subject;
  oMail.Body = body;
  oMail.IsBodyHtml = true;
  oMail.Priority = MailPriority.Normal;
  oMail.Sender = from;
  s = new SmtpClient(smtpServer);
  s.ServicePoint.ConnectionLeaseTimeout = 0;
  if (s != null)
  {
     s.Send(oMail);
  }
  oMail.Dispose();
  s = null;
}

i am sending over 60,000 email using this code,now my problem some recipient gets email right away but some of them gets after few minutes and some of them gets even after few hours and may be many of them gets lost before reaching to destination. and my This Issue is still unanswered. i really need help in this. i am stuck. thanks

A: 

Actually that makes sense. Don’t you think that by sending 60K emails in a very short timeframe your are likely to be considered as a spammer? Delaying the emails like StrongMail does is a good way to prevent you from being banned by the ISPs.

Nip
+1  A: 

Try the following: your MailMessage needs to be in a using block. Also, you don't need a new SmtpClient for each message. You certainly don't need to set it to null! This is not VB6.

SmtpClient smtpClient = new SmtpClient(smtpServer);
smtpClient.ServicePoint.ConnectionLeaseTimeout = 0;
for (int i = 0; i < number; i++)
{
    MailAddress to = new MailAddress(iMail.to);
    MailAddress from = new MailAddress(iMail.from, iMail.displayName);
    string body = iMail.body;
    string subject = iMail.sub;
    using (MailMessage mailMessage = new MailMessage(from, to))
    {
        mailMessage.Subject = subject;
        mailMessage.Body = body;
        mailMessage.IsBodyHtml = true;
        mailMessage.Priority = MailPriority.Normal;
        mailMessage.Sender = from;
        smtpClient.Send(mailMessage);
    }
}

Suggestion: don't name variables things like oMail. We know it's an object. Most things are. There's nothing special about objects anymore.

John Saunders
Thanks John, but as per Documentation of ServicePoint.ConnectionLeaseTimeout if i set it to 0, smtp server will close connection after receiving first request, am i right? btw when this connection get close?
Nnp
A: 

the problem isn't likely with your code, but with your SMTP server.

It could be any number of issues, like failing to find MX records in your DNS server (especially if your DNS server is weak, and can't handle the lookups that fast).

I would check your mail server logs, as some indication as to what is happening.

dave wanta