SMTP.MAIL: 操作超时的原因是什么?

发布于 2025-01-07 13:04:05 字数 5175 浏览 0 评论 0原文

我正在创建一个基于 Intranet Web 的应用程序,它将根据管理员的选择向其用户发送通知。

我正确开发了邮件页面,并且它可以在 15 或 20 个用户的情况下正常工作。现在,我有超过 200 个用户,所以当我运行此页面时,我收到以下错误,但我不知道为什么:

异常详细信息:System.Net.Mail.SmtpException:该操作已 超时。

我的代码隐藏 (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("[email protected]", "PMOD Safety Services Portal (PSSP)");

            // In case the mail system doesn't like no to recipients. This could be removed
            //msg.To.Add("[email protected]");

            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();
        }
    }

有什么帮助吗?

I am creating an intranet web-based application that will send notifications to its users depending on the Admin's choice.

I developed the Mailing page correctly and it work fine with 15 or 20 users. Now, I have more than 200 users, so when I run this page I got the following error and I don't know why:

Exception Details: System.Net.Mail.SmtpException: The operation has
timed out.

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("[email protected]", "PMOD Safety Services Portal (PSSP)");

            // In case the mail system doesn't like no to recipients. This could be removed
            //msg.To.Add("[email protected]");

            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();
        }
    }

Any help please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌若浮生 2025-01-14 13:04:05
  1. 确保你的SMTP服务器在线,因为我的测试“telnet MAIL.Aramco.com 25”,它显示服务器不可用。
  2. 作为您的代码,您使用的是 SMTP 服务器但没有身份验证,它是否支持匿名访问?
  1. make sure your SMTP server is online, as my test "telnet MAIL.Aramco.com 25", it displays the server is unavailable.
  2. As your code, you are using SMTP server but no authentication, does it support anonymously access?
够钟 2025-01-14 13:04:05

添加sc.timeout=600000(10分钟)
在发送电子邮件方法中。

如果电子邮件发送需要更多时间才能完成,它可以让您

add sc.timeout=600000(10min)
in send email method.

if email send requires more time to complete the it allows you

夜空下最亮的亮点 2025-01-14 13:04:05

如果这是一个并发问题,那么使用 SendAsync 似乎可以帮助解决您的问题,正如在此 帖子

这里是 MSDN 中的一个示例,可能会有所帮助。您应该将 Send 方法替换为 SendAsync,如此代码所示。

If it is a concurrency issue as it seems maybe using SendAsync can help to solve your probelm as it discussed in this post

Here's an example in MSDN that might help. You should replace the Send method with SendAsync as this code shows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文