如何限制要发送的电子邮件(Azure 电子邮件通信)的数量,C#?

how to limit the number of emails(azure email communication) to send, c#?

提问人:ani H 提问时间:9/24/2023 更新时间:9/24/2023 访问量:40

问:

我正在使用一个控制台应用程序,该应用程序从数据库中读取记录,并相应地使用 Azure 电子邮件通信发送电子邮件。

赛德 ShouldEmailBeSent HasEmailBeenSent 电子邮件发送时间 下一个操作作者 NextActionByUser
1 1 0 S22型 Manager用户
2 1 0 云团队 技术用户
3 1 0 AITeam公司 技术用户
4 1 0 云团队 技术用户

这是我的数据库表“WorkExtended”中的记录。

我做了什么:

我在 NextActionByUser 列“ManagerUser”和“TechUser”中有 2 个类别 如果 ShouldEmailBeSent = 1,那么它会检查 NextActionByUser =“ManagerUser”,然后检查 NextActionBy 以获取经理用户 ID,并使用该用户 ID 从另一个表(UserDetails)获取经理的电子邮件 ID,然后将发送电子邮件。如果 NextActionByUser =“TechUser”(TechUser 有很多团队),NextActionBy =“检查团队名称”,并且每个团队有 3 到 4 名成员,我不想为每个 SEID 发送 3 到 4 封电子邮件,所以我将共享相同 NextActionBy(Team) 的 SEID 分组,这样它只会为共享同一团队的 SEID 发送 3 封电子邮件。

int maxEmailsToSend = 4;
        using (SqlConnection connection = new SqlConnection(dbConnectionString))
        {
            connection.Open();

            string query = "SELECT SEID, ShouldEmailBeSent,NextActionBy,NextActionByUser FROM WorkExtended " + "WHERE ShouldEmailBeSent = 'True' AND HasEmailBeenSent = 'False' AND EmailSentTime IS NULL ";
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    Dictionary<string, List<int>> seidsByTeam = new Dictionary<string, List<int>>();
                    int emailsSentCount = 0;
                    while (reader.Read() && emailsSentCount< maxEmailsToSend)
                    {

                        int seid = Convert.ToInt32(reader["SEID"]);
                        bool shouldemailbesent = Convert.ToBoolean(reader["ShouldEmailBeSent"]);
                        string nextActionByUser = reader["NextActionByUser"].ToString();


                        if (shouldemailbesent)
                        {
                            if (nextActionByUser == "TechUser")
                            {
                                string teamName = GetTeamNameFromNextActionBy(connection, nextActionByUser, seid);
                                if (!seidsByTeam.ContainsKey(teamName))
                                {
                                    seidsByTeam[teamName] = new List<int>();
                                }
                                seidsByTeam[teamName].Add(seid);
                            }
                            else
                            {
                                string managerId = GetManagerUserIdFromNextActionBy(connection, nextActionByUser, seid);
                                string managerEmail = GetEmailForManagerUser(connection, managerId);
                                if (emailsSentCount < maxEmailsToSend)
                                {
                                    await SendEmailToRecipient(seid, managerEmail);
                                    emailsSentCount++;
                                    string updateQuery = "UPDATE WorkExtended SET HasEmailBeenSent = 'True', EmailSentTime = CURRENT_TIMESTAMP WHERE SEID = @SEID";

                                    using (SqlCommand updateCommand = new SqlCommand(updateQuery, connection))
                                    {
                                        updateCommand.Parameters.AddWithValue("@SEID", seid);
                                        updateCommand.ExecuteNonQuery();
                                        Console.WriteLine("Updated SEID: " + seid);
                                    }
                                }                                   
                            }
                        }
                    }
                    foreach (var teamEntry in seidsByTeam)
                    {
                        string teamName = teamEntry.Key;
                        List<int> seidsForTeam = teamEntry.Value;
                        List<string> techUserEmails = GetEmailsForTechUsers(connection, teamName);
                        if (emailsSentCount < maxEmailsToSend)
                        {
                            int remainingEmails = maxEmailsToSend - emailsSentCount;
                            int emailsToSendForTeam = Math.Min(remainingEmails, seidsForTeam.Count);

                            if (emailsToSendForTeam > 0)
                            {
                                await SendGroupEmail(seidsForTeam.GetRange(0, emailsToSendForTeam), techUserEmails);
                                emailsSentCount += emailsToSendForTeam;
                            }
                            string updateQuery = "UPDATE WorkExtended SET HasEmailBeenSent = 'True', EmailSentTime = CURRENT_TIMESTAMP WHERE SEID = @SEIDs";

                            using (SqlCommand updateCommand = new SqlCommand(updateQuery, connection))
                            {
                                updateCommand.Parameters.AddWithValue("@SEIDs", string.Join(",", seidsForTeam.GetRange(0, emailsToSendForTeam)));
                                updateCommand.ExecuteNonQuery();
                                Console.WriteLine($"Updated SEIDs: {string.Join(", ", seidsForTeam.GetRange(0, emailsToSendForTeam))}");
                            }
                        }
                    }
                }
            }
            connection.Close();

我想要的是:

我想限制发送的电子邮件数量。如果我设置 maxEmailsToSend = 4 表示,它只需要发送 4,不能超过这个。例如:根据我的数据库,它发送 SEID 1 的电子邮件并更新数据库 HasEmailBeenSent = 1,EmailSentTime = “电子邮件发送日期”。现在只剩下 3 封电子邮件,然后它检查剩余的记录,将我的 SEID 2 和 4 分组,将电子邮件发送给 CloudTeam 的 3 名成员,并更新我的数据库。 现在已达到电子邮件限制,它不应发送电子邮件。但问题是它还为 SEID 3 发送了电子邮件。我想要这样的东西,如果达到限制,它不应该发送电子邮件。即使它还有 1 封电子邮件仍然在其限制范围内,但下一个记录 (seid) 有一个团队,它必须向 3 个成员发送邮件,它不应该发送。我在我的代码中尝试了一些东西,但我无法弄清楚如何确切地实现这一点。

谢谢。

C# 电子邮件 azure-communication-services

评论

0赞 Ian Kemp 9/24/2023
向我们展示“你尝试过的东西”。Stack Overflow 的存在不是为了为你编写代码,而是为了帮助你使你所写的内容正常工作。
0赞 ani H 9/24/2023
嗨,我知道 Stack Overflow 不是为了为我或任何人编写代码而存在的,我也知道它可以帮助编码人员变得更好。我也发布了代码,这就是我尝试过的,它正在工作,但它不能满足我的条件。我尝试了不同的方法,但我仍然无法弄清楚我错过了什么。我是初学者编码员,我不是像你们一样的专业人士。这就是我寻求帮助的原因。对不起,如果我犯了任何错误。

答: 暂无答案