-1

My Table Data is

JSID    JSName JSEmail      JobTitle  PayFrom PayTo EmpCode JobID
--------------------------------------------------------------------
JS0001  ABC    abc@xyz.com  Devloper  20000   25000 EMP001  JD001
JS0001  ABC    abc@xyz.com  Tester    15000   20000 EMP001  JD002
JS0001  ABC    abc@xyz.com  Designer  10000   15000 EMP001  JD003
JS0002  PQR    pqr@xyz.com  Devloper  20000   25000 EMP001  JD001
JS0002  PQR    pqr@xyz.com  Tester    15000   20000 EMP001  JD002
JS0002  PQR    pqr@xyz.com  Designer  10000   15000 EMP001  JD003

I want to send mail to differnt person eg. ABC and PQR with JobTitle,PayFrom and Payto in single mail. in asp.net code or C# code

Here is my Email sending code which take data from table . . . . . and for loop iterator 6 time and send 3 mail to ABC and 3 mail to PQR But i Want 1 mail to ABC with 3 JobDetails is, is this possible or any solution please help me.

  objJobSchedulerMailDTO.JobSchedulerMailList = objJobSchedulerMailDAL.SelectJob(Action);
                         for (int i = 0; i < objJobSchedulerMailDTO.JobSchedulerMailList.Count; i++)
                         {
                             string email = objJobSchedulerMailDTO.JobSchedulerMailList[i].JSEmail;





                         string htmBody = "<html><head>" +
                                            "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" +
                                            "<title>FitIndia jobs</title>" +
                                             "<script type='text/javascript' src='chrome-extension://aadgmnobpdmgmigaicncghmmoeflnamj/ng-inspector.js'></script></head>" +
                                             "<body style='margin:0 auto; font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#525252;'>" +
                                             "<div class='main' style='width:674px; padding:0px; margin:0 auto; border:1px solid #eee;'>" +
                                             "<div class='logo1' style='width:674px; padding:15px 0px; border-bottom:1px solid #eee;'>" +

                                             "</div>" +
                                             "<div class='content2' style='width:674px; padding:10px 0px; clear:both;'>" +
                                             "<div class='content1' style='font-size:15px; padding:15px 10px; width:654px;'>Hi " + objJobSchedulerMailDTO.JobSchedulerMailList[i].JSName +,<br>" +

                                             "</div>" +
                                             "<div class='clear' style='clear:both;'></div>" +
                                             "<div class='content2' style='width:674px; padding:5px 0px; float:left;'>" +
                                             "<div class='username-box1' style='font-size:15px; width:200px; padding:0px 2px 0px 10px; float:left; font-weight:600;'>Job Title : </div>" +
                                             "<div class='username-box1' style='font-size:15px; width:400px; float:left;'>" + objJobSchedulerMailDTO.JobSchedulerMailList[i].JobTitle + "</div>" +
                                             "</div>" +
                                             "<div class='content2' style='width:674px; padding:5px 0px; float:left;'>" +
                                             "<div class='username-box1' style='font-size:15px; width:200px; padding:0px 2px 0px 10px; float:left; font-weight:600;'>Pay Scale : </div>" +
                                             "<div class='username-box1' style='font-size:15px; width:400px; float:left;'>" + "INR: " + objJobSchedulerMailDTO.JobSchedulerMailList[i].PayFrom + " - " + objJobSchedulerMailDTO.JobSchedulerMailList[i].PayTo + "</div>" +
                                             "</div>" +

                                             "</div>" +
                                             "</div>" +
                                             "</div>" +
                                             "<div class='content2' style='width:674px; padding:10px 0px; clear:both; line-height: 25px;'>" +
                                             "<div class='content1' style='font-size:15px; padding:15px 10px; width:654px;'>Thanks &amp; Regards<br>" +


                                             "</div>" +
                                             "</div>" +
                                             "</div>" +
                                             "</body></html>";

                        string fromEmail = Convert.ToString(ConfigurationSettings.AppSettings["Email"]);
                        string fromPassword = Convert.ToString(ConfigurationSettings.AppSettings["Password"]);
                        string toEmail = email;


                         try
                         {
                             using (MailMessage mm = new MailMessage(fromEmail, toEmail))
                             {

                                 mm.Subject = "Mail";
                                 mm.Body = htmBody;

                                 mm.IsBodyHtml = true;
                                 SmtpClient smtp = new SmtpClient();
                                 smtp.Host = "smtp.gmail.com";
                                 smtp.EnableSsl = true;
                                 NetworkCredential net = new NetworkCredential(fromEmail, fromPassword);
                                 smtp.UseDefaultCredentials = true;
                                 smtp.Credentials = net;
                                 smtp.Port = 587;
                                 smtp.Send(mm);




                             }
                         }
                         catch (Exception ex)
                         {

                             throw;
                         }

                     }

                 }
               catch (Exception)
               {
                   throw;
               }
               return objJobSchedulerMailDTO;
     }

this is in MVC4 Controller

Tushar
  • 182
  • 3
  • 19

1 Answers1

1

You need to take distinct Email Id's from database check below code I have modify (If you require help for distinct you list check this link How to distinct element from list ):

 objJobSchedulerMailDTO.JobSchedulerMailList = objJobSchedulerMailDAL.SelectJob(Action);
 var distinctList = bjJobSchedulerMailDTO.JobSchedulerMailList.DistinctBy(x => x.JSEmail).ToList();
for (int i = 0; i < distinctList.Count; i++)
{
                             string email = distinctList[i].JSEmail;





                         string htmBody = "<html><head>" +
                                            "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" +
                                            "<title>FitIndia jobs</title>" +
                                             "<script type='text/javascript' src='chrome-extension://aadgmnobpdmgmigaicncghmmoeflnamj/ng-inspector.js'></script></head>" +
                                             "<body style='margin:0 auto; font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#525252;'>" +
                                             "<div class='main' style='width:674px; padding:0px; margin:0 auto; border:1px solid #eee;'>" +
                                             "<div class='logo1' style='width:674px; padding:15px 0px; border-bottom:1px solid #eee;'>" +

                                             "</div>" +
                                             "<div class='content2' style='width:674px; padding:10px 0px; clear:both;'>" +
                                             "<div class='content1' style='font-size:15px; padding:15px 10px; width:654px;'>Hi " + distinctList[i].JSName +,<br>" +

                                             "</div>" +
                                             "<div class='clear' style='clear:both;'></div>" +
                                             "<div class='content2' style='width:674px; padding:5px 0px; float:left;'>" +
                                             "<div class='username-box1' style='font-size:15px; width:200px; padding:0px 2px 0px 10px; float:left; font-weight:600;'>Job Title : </div>" +
                                             "<div class='username-box1' style='font-size:15px; width:400px; float:left;'>" + distinctList[i].JobTitle + "</div>" +
                                             "</div>" +
                                             "<div class='content2' style='width:674px; padding:5px 0px; float:left;'>" +
                                             "<div class='username-box1' style='font-size:15px; width:200px; padding:0px 2px 0px 10px; float:left; font-weight:600;'>Pay Scale : </div>" +
                                             "<div class='username-box1' style='font-size:15px; width:400px; float:left;'>" + "INR: " + distinctList[i].PayFrom + " - " + distinctList[i].PayTo + "</div>" +
                                             "</div>" +

                                             "</div>" +
                                             "</div>" +
                                             "</div>" +
                                             "<div class='content2' style='width:674px; padding:10px 0px; clear:both; line-height: 25px;'>" +
                                             "<div class='content1' style='font-size:15px; padding:15px 10px; width:654px;'>Thanks &amp; Regards<br>" +


                                             "</div>" +
                                             "</div>" +
                                             "</div>" +
                                             "</body></html>";

                        string fromEmail = Convert.ToString(ConfigurationSettings.AppSettings["Email"]);
                        string fromPassword = Convert.ToString(ConfigurationSettings.AppSettings["Password"]);
                        string toEmail = email;


                         try
                         {
                             using (MailMessage mm = new MailMessage(fromEmail, toEmail))
                             {

                                 mm.Subject = "Mail";
                                 mm.Body = htmBody;

                                 mm.IsBodyHtml = true;
                                 SmtpClient smtp = new SmtpClient();
                                 smtp.Host = "smtp.gmail.com";
                                 smtp.EnableSsl = true;
                                 NetworkCredential net = new NetworkCredential(fromEmail, fromPassword);
                                 smtp.UseDefaultCredentials = true;
                                 smtp.Credentials = net;
                                 smtp.Port = 587;
                                 smtp.Send(mm);




                             }
                         }
                         catch (Exception ex)
                         {

                             throw;
                         }

                     }

                 }
               catch (Exception)
               {
                   throw;
               }
      return objJobSchedulerMailDTO;
}

Cheers!!

Laxman Gite
  • 2,248
  • 2
  • 15
  • 23