c# - Send Bulk Email to Groups Using SMTP -


please consider following:

1. departments table:


deparmentid | departmentname  ____________________________ 1           | sales 2           | finance 3           | i.t 4           | hr 5           | management 

2. employees table

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 

i want send email via smtp employees belonging department group. if database 50 employees, function so:

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 assigned departmentid     } } 

but database housing hundreds of employees need emailed periodically per departmentid in given scenario - how can achieve using smtpclient?

so logic requires send email users belonging specific department. if send management sarah , mark should receive email. c# windows forms application.

  1. stored procedure:

    create proc spmapuserstodepartments @departmentid [smallint]     begin     select email employeesperdepartment departmentid=@departmentid end go  -- test exec spmapuserstodepartments 5 
  2. return list of employees assigned department

    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. iterate through 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";   }      

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -