IIS error sending newsletter using ASP.NET MVC

I have a problem sending a newsletter (up to about 5,000 people) in my mvc-helper class.

public static void SendNewsletter(string fromAccount, string subject, string eMailText)
{
    var userEMailAddresses = new List<string>();

    using (var dbContext = new dbEntities())
    {
        userEMailAddresses =
            dbContext
                .User
                    .Where(w =>
                        !w.IsDeactivated &&
                        !w.IsBlacklisted)
                    .Select(s =>
                        s.UserName)
                    .ToList();
    }

    new Thread(() =>
    {
        for (int i = 0; i < userEMailAddresses.Count; i++)
        {
            SendMail(fromAccount, userEMailAddresses[i], subject, eMailText);
        }
    }).Start();
}

This is my function that will be called in the controller. The following block code is the submit function.

public static void SendMail(string fromAccount, string toAccount, string subject, string eMailText)
{
    var fromEmailAddress = new EMailModel(fromAccount);

    var body = string.Empty;
    using (var sr = new StreamReader(HostingEnvironment.MapPath("\\App_Data\\Templates\\") + "EMailTemplate.txt"))
    {
        body = sr.ReadToEnd();
    }

    body = body.Replace("%%Subject%%", subject);
    body = body.Replace("%%Body%%", eMailText);
    body = body.Replace("%%Year%%", DateTime.Now.Year.ToString());

    using (MailMessage mail = new MailMessage(fromEmailAddress.EMailAddress, toAccount))
    {
        mail.Subject = subject;
        mail.Body = WebUtility.HtmlDecode(body);
        mail.IsBodyHtml = true;

        using (SmtpClient smtp = new SmtpClient(Statics.SmtpClient, Statics.SmtpPort))
        {
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(fromEmailAddress.EMailAddress, fromEmailAddress.Password);
            smtp.EnableSsl = Statics.SslEnabled;
            try
            {
                smtp.Send(mail);
                Thread.CurrentThread.Join(1000);
            }
            catch (Exception ex) {
                throw ex;
            }
        }
    }
}

After about 1300 emails, my IIS will receive the following error:

ErrorImage

How can I solve this problem? I need a newsletter system ...

+4
source share
1 answer

I found a lot of errors why this is not working.

MailEnable. MailEnableAdmin → → → → Rightclick SMTP Settings → Security-Tab (1000). .

E-Mail-. E-Mail-Check-Funtion:

    private static bool IsValidEMail(string eMailAddress)
    {
        if (Regex.IsMatch(eMailAddress, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase) && new EmailAddressAttribute().IsValid(eMailAddress))
            return true;

        return false;
    }

    public static void SendMail(string fromAccount, string toAccount, string subject, string eMailText)
    {
        if (!IsValidEMail(toAccount))
        {
            var invalidEMailAddress = toAccount;

            toAccount = "administrator@YOUR-DOMAIN.com";
            subject = "E-Mail-Address is invalid";
            eMailText = String.Format("Dear Administrator,<br><br>the following E-Mail-Address is invalid:<br><br>{0}", invalidEMailAddress);
        }

        var fromEmailAddress = new EMailModel(fromAccount);

        var body = string.Empty;
        using (var sr = new StreamReader(HostingEnvironment.MapPath("\\App_Data\\Templates\\") + "EMailTemplate.txt"))
        {
            body = sr.ReadToEnd();
        }

        body = body.Replace("%%Subject%%", subject);
        body = body.Replace("%%Body%%", eMailText);
        body = body.Replace("%%Year%%", DateTime.Now.Year.ToString());

        using (MailMessage mail = new MailMessage(fromEmailAddress.EMailAddress, toAccount))
        {
            mail.Subject = subject;
            mail.Body = WebUtility.HtmlDecode(body);
            mail.IsBodyHtml = true;

            using (SmtpClient smtp = new SmtpClient(Statics.SmtpClient, Statics.SmtpPort))
            {
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(fromEmailAddress.EMailAddress, fromEmailAddress.Password);
                smtp.EnableSsl = Statics.SslEnabled;
                try
                {
                    smtp.Send(mail);
                    Thread.CurrentThread.Join(1000);
                }
                catch (Exception) { }
            }
        }
    }

SendNewsletter - . .

, !

0

All Articles