如何防止代码在asp.net中发送重复的电子邮件?

时间:2019-07-20 05:21:09

标签: c# asp.net iis

我的网站上有一个代码,可以在用户单击按钮时发送电子邮件。由于某些原因,该代码开始发送大量重复的电子邮件。实际上,它在达到GoDaddy的每日限制(即5000封电子邮件!!!

有什么办法可以防止这种情况发生? SMTP超时在这种情况下有帮助吗?

    <color name="darkRed">#F48B5E</color>
    <color name="lightRed">#E9A191</color>

1 个答案:

答案 0 :(得分:0)

尝试一下

try
    {
        bool flag = false;
if(!flag)
{        
        SmtpClient client = new SmtpClient("sg2nlvphout-v01.shr.prod.sin2.secureserver.net", 25);
        client.EnableSsl = false;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("email@domain.com", "password");
        MailMessage msgobj = new MailMessage();
        msgobj.To.Add(email);
        msgobj.From = new MailAddress("email@domain.com");
        msgobj.Subject = "Subject";
        msgobj.Body = body;
        AlternateView altView = AlternateView.CreateAlternateViewFromString(msgobj.Body, null, MediaTypeNames.Text.Html);

        msgobj.AlternateViews.Add(altView);

        client.Send(msgobj);
        flag = true;
}
    }
    catch (Exception ex)
    {

    }