0

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

Community
  • 1
  • 1
Nnp
  • 1,813
  • 7
  • 36
  • 62
  • 1
    Chances are your smtp server has a setting stating you can only send X emails per X minutes, or something along those lines. To confirm, append the current datetime to the body of the email, and compare to the recieved date. – PostMan Feb 09 '10 at 01:12
  • thanks PostMan, in my code "smtpServer" is a static ip(3rd party) that means it does not use my local smtp server, right? if yes then i can i check setting for smtp server? – Nnp Feb 09 '10 at 01:26
  • No you'll have to contact them directly, I would recommend the putting the datetime in the body of the email, even if you send it to yourself. – PostMan Feb 09 '10 at 01:31
  • i have run the test program at 2 Pm with 50 emails. its 6Pm i havent got all them. some time i get 8 emails some time 10 at any given point of time.and i use StrongMail as a smtp server. they are specialize in mass email. does this make any sense. – Nnp Feb 09 '10 at 02:29
  • 1
    If they all get your message, then there's nothing wrong with your code. Your code could fail to send the message, but there's no way the code you show could delay a message. – John Saunders Feb 09 '10 at 03:09
  • If you can check the logs of the SMTP server, or the headers of the received mail, you might find evidence of greylisting. – Josh Lee Feb 09 '10 at 13:51

3 Answers3

3

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
  • 160,644
  • 26
  • 247
  • 397
  • 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 Feb 09 '10 at 17:47
0

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.

Romhein
  • 2,126
  • 2
  • 17
  • 17
0

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
  • 7,164
  • 3
  • 29
  • 29