电子邮件发送给包含表数据的一个人

时间:2017-06-30 09:40:07

标签: c# asp.net-mvc

我的表格数据

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

我想发送邮件给不同的人,例如。 ABC和PQR与JobTitle,PayFrom和Payto在一个邮件中。在asp.net代码或C#代码

这是我的电子邮件发送代码,它从表中获取数据。 。 。 。 。 并为循环迭代器6次并发送3封邮件给ABC和3封邮件到PQR 但是,我希望通过3个JobDetails向ABC发送1封邮件, 这是可能的还是任何解决方案请帮助我。

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

这是在MVC4控制器

1 个答案:

答案 0 :(得分:1)

您需要从我修改过的代码中检查不同的电子邮件ID&#39> 如果您需要帮助以便列出不同的代码,请检查此链接 {{ 3}}):

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

<强>干杯!!