I need to develop a mailing system in the intranet web-based application that I am working on it for my company. I am developing it with ASP.NET and C#. The purpose of this system is to let the admin to be able to send emails to the users. I developed this system and I tested for 25 users and it works fine.

Now, I have 386 users in the database, so when I tried to send them emails, I got the following error:
> Exception Details: System.Net.Mail.SmtpException: The operation has
> timed out.

I think this is because of ISP in my company blocked sending email to many users after a certain number of milliseconds. I tried to use SendAsync but I found it will not benefit me.

Also, I tried to maximize the execution timeout in the Web.config file as folloiwng:

<location path="Email4.aspx">
            <system.web>
                    <httpRuntime executionTimeout="180"/>
            </system.web>
    </location>

but I failed. **So how to fix this problem?**

My Code-Behind (C#):

protected void Page_Load(object sender, EventArgs e)
        {
            SendEmailTOAllUser();
        }
    
    
        protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
        {
            SmtpClient sc = new SmtpClient("MAIL.companyDomainName.com");
            try
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("pssp@companyDomainName.com", "PMOD Safety Services Portal (PSSP)");
    
                // In case the mail system doesn't like no to recipients. This could be removed
                //msg.To.Add("pssp@companyDomainName.com");
    
                msg.Bcc.Add(toAddresses);
                msg.Subject = MailSubject;
                msg.Body = MessageBody;
                msg.IsBodyHtml = isBodyHtml;
                //Response.Write(msg);
                sc.Send(msg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
    
        }
    
        protected void SendEmailTOAllUser()
    
        {
            string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspEmail;Integrated Security=True";
    
            using (SqlConnection conn = new SqlConnection(connString))
            {
                var sbEmailAddresses = new System.Text.StringBuilder(1000);
                string quizid = "";
    
                // Open DB connection.
                conn.Open();
    
                string cmdText = "SELECT MIN (QuizID) As mQuizID FROM dbo.QUIZ WHERE IsSent <> 1";
                using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                {
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            // There is only 1 column, so just retrieve it using the ordinal position
                            quizid = reader["mQuizID"].ToString();
    
                        }
                    }
                    reader.Close();
                }
    
                string cmdText2 = "SELECT Username FROM dbo.employee";
                using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
                {
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            var sName = reader.GetString(0);
                            if (!string.IsNullOrEmpty(sName))
                            {
                                if (sbEmailAddresses.Length != 0)
                                {
                                    sbEmailAddresses.Append(",");
                                }
                                // Just use the ordinal position for the user name since there is only 1 column
                                sbEmailAddresses.Append(sName).Append("@companyDomainName.com");
                            }
                        }
                    }
                    reader.Close();
                }
    
                string cmdText3 = "UPDATE dbo.Quiz SET IsSent = 1 WHERE QuizId = @QuizID";
                using (SqlCommand cmd = new SqlCommand(cmdText3, conn))
                {
                    // Add the parameter to the command
                    var oParameter = cmd.Parameters.Add("@QuizID", SqlDbType.Int);
                    // Get a local copy of the email addresses
                    var sEMailAddresses = sbEmailAddresses.ToString();
    
    
                        string link = "<a href='http://pmv/pssp/StartQuiz.aspx?testid=" + quizid + "'> Click here to participate </a>";
                        string body = @"Good day, <br /><br />
                                    <b> Please participate in the new short safety quiz </b>"
                                            + link +
                                            @"<br /><br />
                                Also, give yourself a chance to gain more safety culture by reading the PMOD Newsletter.
                                <br /> <br /><br /> <br />
                                This email was generated using the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal (PSSP) </a>. 
                                Please do not reply to this email.
                                ";
    
                        SendEmail(sEMailAddresses, "", "Notification of New Weekly Safety Quiz", body, true);
    
                        // Update the parameter for the current quiz
                        oParameter.Value = quizid;
                        // And execute the command
                        cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
        }

Can you send to 100 at a time and just send multiple mails?
Can you have the admin create 1 or more PDLs to minimize babe conversions?
Is there more than one SMTP server that can share the load?

I would think that if an ISP saw you sending out 100+ emails at once it would think you are a spam bot. If you are a legitimate company, you could probably contact the ISP and ask them to allow it. You could try splitting up the load (send out to all the subscribers throughout the course of a whole day).

Just to be clear, I mean 100 recipients -- which in your scenario would result in 4 emails.
That might still be a problem with that many recipients of the same domain.
There is probably a "sweet-spot" between number of recipients and number of actual messages the ISP or SMTP Server will allow without failing for one reason or other.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.