-1

I am working on a project with sending an email notification to assigned users. The email notification is working fine and was sent to assigned users. But the problem is, the email sent shows duplicate names see screenshot. The sent email is not duplicate, only the names are duplicate.

Can you help me resolve this? Thank you for the response.

//Send Email Notification to Assigned Users
    public void SendEmail(Guid[] UsersID, string place,
                                            int? hn,
                                            int? age,
                                            string gender,
                                            string persons_involved,
                                            string patientName_incident,
                                            string narrative_report,
                                            DateTime? created_date,
                                            Guid IRID)
    {
        var Host = ConfigurationManager.AppSettings["smtpclient"].ToString();
        var FromEmailID = ConfigurationManager.AppSettings["FromEmail"].ToString();
        var port = ConfigurationManager.AppSettings["port"].ToString();
        var username = ConfigurationManager.AppSettings["username"].ToString();
        var password = ConfigurationManager.AppSettings["password"].ToString();

        var incident_name = (from i in db.All_Issues
                             select i.PatientName_Incident);

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(FromEmailID);
        mail.Subject = "Incident Report 2017 - Incident Name: " + patientName_incident +  " has been assigned to you.";
        mail.Body = EmailMessage(place,
                                hn,
                                age,
                                gender,
                                persons_involved,
                                patientName_incident,
                                narrative_report,
                                created_date,
                                IRID);

        mail.IsBodyHtml = true;
        mail.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");

        SmtpClient smtpclient = new SmtpClient();
        smtpclient.Host = Host;
        smtpclient.Port = Convert.ToInt32(port);
        smtpclient.Credentials = new System.Net.NetworkCredential(username, password);

        List<users_ref> users = db.users_ref.ToList();
        if (UsersID != null)
        {
            foreach (var Users in UsersID)
            {
                foreach (var user in UsersID)
                {
                    foreach (var usersList in users)
                    {
                        if (Users == usersList.UsersID)
                        {
                            mail.To.Add(usersList.Email);
                        }
                    }
                }
            }

            try
            {
                smtpclient.Send(mail);
            }
            catch (InvalidOperationException ex)
            {
                ModelState.AddModelError("", ex);
            }
        }
    }
da26
  • 7
  • 5

2 Answers2

0

just check you are looping two times for UsersId. I think that the reason you are getting two names.

Tonmoy Saha
  • 652
  • 1
  • 5
  • 13
0

Already resolved the problem. You guys are right, the problem is in my looping. It is now fixed.

List<users_ref> users = db.users_ref.ToList();
        if (UsersID != null)
        {
            foreach (var Users in UsersID)
            {
                    foreach (var usersList in users)
                    {
                        if (Users == usersList.UsersID)
                        {
                            mail.To.Add(usersList.Email);
                        }
                    }
            }

            try
            {
                smtpclient.Send(mail);
            }

Thank you guys for the response.

da26
  • 7
  • 5