每当我通过smtp发送邮件时如何增加变量

时间:2018-08-17 07:55:17

标签: c# .net smtp increment

我有一个邮递员,这里是代码:

 private static int i=0; 
 protected void btnSubmit_Click(object sender, EventArgs e)
 {    
     ++i; //i want to increment this variable

            {
                SendHTMLMail();
            }


            void SendHTMLMail()
            {
                StreamReader reader = new StreamReader(Server.MapPath("~/one.html"));
                string readFile = reader.ReadToEnd();
                string myString = "";
                myString = readFile;



                MailMessage Msg = new MailMessage();

                Msg.From = new MailAddress(txtUsername.Text);

                Msg.To.Add(txtTo.Text);
                Msg.Subject = txtSubject.Text;
                Msg.Body = myString.ToString();
                Msg.IsBodyHtml = true;

                if (fuAttachment.HasFile)
                {
                    string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);

                    Msg.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
                }

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                Msg = null;
                ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);

                // Request both failure and success report
                Msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess;

                int emailsSent = 0;

                try
                {
                    Console.WriteLine("start to send email ...");
                    smtp.Send(Msg);
                    emailsSent++;
                    Console.WriteLine("email was sent successfully!");

                }
                catch (Exception ex)
                {
                    Console.WriteLine("failed to send email with the following error:");
                    Console.WriteLine(ex.Message);
                }
            }
        }

在上面的代码中,我有一个变量“ i”,我想在每次发送邮件时对其进行递增。现在我面临的问题是,仅当我一次又一次发送邮件时(当我在本地主机中的aspx页打开时),“ i”才会增加。当我关闭aspx页面后,重新打开它并再次发送邮件,然后变量“ i”再次增加到1,而不是4或5。

2 个答案:

答案 0 :(得分:0)

行为会更改您放置此代码的位置。如果在ASPX页面中,则在运行时重新编译该页面时,您将丢失静态数据。如果它在DLL文件中,则每当应用程序/ IIS池回收时,您都将丢失值。您需要将最终值保存到持久性存储(即数据库)中。下次需要它们时,必须从数据库检索,将其递增,然后再次保存。 注意,Web应用程序是多线程的,而静态变量不是线程安全的。如果两个线程同时修改同一变量,您将陷入混乱。使用锁定机制来访问多线程应用程序中的静态变量。

答案 1 :(得分:0)

您需要为其定义一个单独的静态类-(因为如果您在asp.net生命周期中点击了刷新/重新加载页面,则整个页面将与对象一起被重新加载。)

定义一个静态类(或带有其构造函数的非静态类),该类具有每次发送邮件时都会递增的变量/属性。

public static class Mail
{
       private static int mailCount;

       public static void MailSent()
       {
          mailCount++;
       }

       public static int GetMailCount()
       {
          return mailCount;
       }
}

现在在您的按钮中单击使用静态方法来递增和检索mailCount-

protected void btnSubmit_Click(object sender, EventArgs e)
{
     Mail.MailSent(); // increments every time you send mail
     // to check how many mails sent in debug console
     System.Diagnostics.Debug.WriteLine(Mail.GetMailCount()); 
     //... YOU MAILING CODE
}

enter image description here