重复的收件人姓名显示在已发送的电子邮件通知asp.net mvc中

时间:2017-06-21 05:19:02

标签: asp.net-mvc email-notifications

我正在开发一个项目,向分配的用户发送电子邮件通知。电子邮件通知工作正常,并已发送给指定的用户。但问题是,发送的电子邮件显示重复的名称{​​{3}}。发送的电子邮件不重复,只有名称重复。

你可以帮我解决这个问题吗? 谢谢你的回复。

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

2 个答案:

答案 0 :(得分:0)

只是检查你是否为UsersId循环了两次。我认为你得到两个名字的原因。

答案 1 :(得分:0)

已经解决了这个问题。你们是对的,问题出在我的循环中。现在已修复。

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

谢谢大家的回应。