0

see the below code which add mail to address in foreach loo. so i like to know how many maximum email address i can add to MailAddress class thanks

var list = from c in context.Emails orderby c.EmailAddress select c.EmailAddress;
MailMessage mail = new MailMessage();
try
{

    mail.From = new MailAddress(txtfrom.Text);
    foreach (var c in list)  
    {  
        mail.To.Add(new MailAddress(c.ToString()));
    }
    mail.Subject = txtSub.Text;
    mail.IsBodyHtml = true;
    mail.Body = txtBody.Text;
    if (FileUpload1.HasFile)
    {
        mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
    }
    SmtpClient smtp = new SmtpClient();
    smtp.Send(mail); 
}
catch (Exception)
{
    //exception handling
}
Mou
  • 15,673
  • 43
  • 156
  • 275
  • 2
    MailMessage.To is a MailAddressCollection, so I don't think that you have a hard coded limit here. But what is the reaction of your email provider to a very long list of addresses is another story. Waiting to be corrected. – Steve Jun 14 '14 at 19:38
  • 1
    I think there is no restriction on it. If any, it is depends on your mail server. – Hamlet Hakobyan Jun 14 '14 at 19:38
  • As asked this question is duplicate of "array/list limits in.Net" questions. You likely more interested about practical limits, but as pointed out it would depend by provider and likely be significantly smaller than theoretical limit on collection size. – Alexei Levenkov Jun 14 '14 at 21:08

1 Answers1

2

Infinite I guess? It's a MailAddressCollection

Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
Pantelis
  • 2,060
  • 3
  • 25
  • 40
  • out of curiosity i like to ask can i assign 10 million email address to MailAddress collection class ? – Mou Jun 14 '14 at 20:16
  • Sorry, this probably wasn't a proper answer. Like the other guys mentioned, your mail provider might limit this. Also infinite isn't actually possible, since after a vast amount of emails and depending on the technology, you might exceed memory usage. But you can always step it in a loop, thats how I'd do it. – Pantelis Jun 14 '14 at 20:55