使用SMTP

时间:2017-07-26 06:44:20

标签: c# sql-server smtp

请考虑以下事项:

1。部门表:

DeparmentId | DepartmentName 
____________________________
1           | Sales
2           | Finance
3           | I.T
4           | HR
5           | Management

2。员工表

EmployeeId  | EmployeeName  | EmployeeEmail      | DepartmentId
________________________________________________________________
1           | Sarah         | sarah@domain.com   | 2
2           | David         | david@domain.com   | 5
3           | Mark          | mark@domain.com    | 5
4           | John          | john@domain.com    | 1
5           | Arthur        | arthur@domain.com  | 3

我想通过SMTP向属于某个部门组的员工发送电子邮件。如果这是一个拥有50名员工的数据库,我可以像这样查找功能:

private void EmailEmployeeDepartment(int DeptartmentId)
{
    SmtpClient mailClient = new SmtpClient("HostName");
    MailMessage msgMail = new MailMessage(); 

    if(DeptartmentId == 5)
    {
        var mailCollection = new MailAddressCollection()
        {
           new MailAddress("sarah@domain.com", "Sarah"),
           new MailAddress("mark@domain.com", "Mark")
        }; 

        foreach(var sendMailTo in mailCollection) 
        {
           msgMail.To.Add(sendMailTo);
        }

        msgMail.Subject = "Subject Text";
        msgMail.Body = "Email text";
        msgMail.IsBodyHtml = true;
    }
    else if(DeptartmentId == 1)
    {
        //Map other employees belonging to assigned DepartmentId
    }
}

但是对于一个容纳数百名员工的数据库,需要按照我们在给定方案中的DepartmentId定期发送电子邮件 - 如何使用SMTPClient实现此目的?

所以基本上我的逻辑要求我向属于特定部门的所有用户发送电子邮件。如果我发送到Management,则只有SarahMark才会收到该电子邮件。这适用于我的C#Windows窗体应用程序。

1 个答案:

答案 0 :(得分:0)

  1. 存储过程:

    CREATE PROC spMapUsersToDepartments
    @DepartmentId [smallint]    
    AS
    BEGIN
        SELECT email FROM EmployeesPerDepartment WHERE DepartmentId=@DepartmentId
    END
    GO
    
    -- To test
    EXEC spMapUsersToDepartments 5
    
  2. 返回分配给部门的员工列表

    private List<string> GetEmailPerDepartmentGroup(int DepartmentId)
    {
        List<string> result = new List<string>();
        myConnectionString = ConfigurationManager.ConnectionStrings["FSK_ServiceMonitor_Users_Management.Properties.Settings.FSK_ServiceMonitorConnectionString"].ConnectionString;
        using (mySQLConnection = new SqlConnection(myConnectionString))
        {
            mySQLCommand = new SqlCommand("spMapUsersToDepartments", mySQLConnection);
            mySQLCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter = new SqlParameter("@DepartmentId", DepartmentId);
            mySQLCommand.Parameters.Add(parameter);
            mySQLConnection.Open();
            mySQLDataReader = mySQLCommand.ExecuteReader();
            while (mySQLDataReader.Read())
            {
                result.Add(mySQLDataReader["email"].ToString());
            }
        }
        return result;
    }
    
  3. 遍历GetEmailPerDepartmentGroup

      private void EmailEmployeeDepartment(int DeptartmentId)
      {
        SmtpClient mailClient = new SmtpClient("HostName");
        MailMessage msgMail = new MailMessage(); 
    
        foreach (var temp in GetEmailPerDepartmentGroup(5))
        {
            msgMail.To.Add(temp);
        }
    
        msgMail.Subject = "Subject Text";
        msgMail.Body = "Email text";
      }     
    
相关问题