-3

我编写了一个代码,每当用户从前端添加内容时,它就会向管理员发送邮件以进行审核.....问题是有时管理员会收到两封相同内容的邮件。

下面是我的代码

            MailMessage mail = new MailMessage();

            string mailto = ConfigurationManager.AppSettings["adminStoryEmail"].ToString();
            mail.To.Add(mailto);
            //mail.To.Add("vidyasagar.patil@viraltech.in");
            mail.From = new MailAddress(ConfigurationManager.AppSettings["fromEmail"]);
            mail.Subject = ConfigurationManager.AppSettings["email_subject"];
            if (uploadedpath != "")
            {
                mail.Body = "Email ID : " + txtEmail.Text + "<br /> Title : " + txtStoryTitle.Text + "<br />" + " Download :   " + " http://www.achievewithdell.in/uploads/" + uploadedpath + "<br />";
                if (story != "")
                {
                    mail.Body += "New story has been added" + " http://www.achievewithdell.in/admin/ManageStory.aspx";
                }
            }
            else
            {
                mail.Body = "Email ID : " + txtEmail.Text + "<br /> Title : " + txtStoryTitle.Text + " <br />";
                if (story != "")
                {
                    mail.Body += "New story has been added" + " http://www.achievewithdell.in/admin/ManageStory.aspx";
                }
            }
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["smtp_host"];  //Or Your SMTP Server Address
            smtp.Port = 25;
            smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtp_userid"], ConfigurationManager.AppSettings["smtp_password"]); //Or your Smtp Email ID and Password
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            smtp.Send(mail);
4

2 回答 2

0

Debug and make sure smtp Send() method is not called twice, put a break point on that line.

于 2013-04-10T06:35:27.883 回答
0

您说有时电子邮件会发送两次。这表明您给我们的代码很好,问题可能在于调用该代码的内容。

消除重复的一种方法是将要发送的邮件排队,可能在数据库中(存储:收件人、发件人、主题、正文等)。然后定期遍历要发送的邮件,忽略重复邮件并标记已发送的邮件,以免再次发送。

正如另一张海报所建议的那样,如果无法像这样对您的应用程序进行重构,请退出调试器并设置断点。根据您的 Visual Studio 版本,您可以使用断点命中计数,以便您仅在第二次到达断点处。

于 2015-09-16T07:59:59.317 回答