C# 如何在每次通过smtp发送邮件时增加变量
我有一封邮件,下面是代码:C# 如何在每次通过smtp发送邮件时增加变量,c#,.net,smtp,increment,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()
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',我想在每次发送邮件时增加它。现在我面临的问题是,只有当我在localhost中打开我的aspx页面时,一次又一次地发送邮件时,“i”才会增加。一旦我关闭我的aspx页面,重新打开它并再次发送邮件,那么变量“i”再次增加到1,而不是说4或5 在放置此代码的位置,行为会发生变化。如果它在ASPX页面中,那么每当运行时重新编译该页面时,就会丢失静态数据。如果它位于DLL文件中,则每当应用程序/IIS池回收时,都会丢失值。您需要将最终值保存到持久存储(即数据库)中。下次需要它们时,必须从数据库中检索,增加它的值,然后再次保存。
注意,web应用程序是多线程的,静态变量不是线程安全的。如果两个线程同时修改同一个变量,您将陷入混乱。使用锁定机制访问多线程应用程序中的静态变量。您需要为其定义一个单独的静态类-(因为如果在asp.net生命周期中点击刷新/重新加载页面,整个页面将与对象一起重新加载。) 定义一个静态类(或调用其构造函数的非静态类),该类的变量/属性将在每次发送邮件时递增
public static class Mail
{
private static int mailCount;
public static void MailSent()
{
mailCount++;
}
public static int GetMailCount()
{
return 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
}
这是预期的行为,因为这是一个web应用程序。由于不活动,它正在被web服务器回收。下次您请求页面时,您将获得新的应用程序实例-这是新的应用程序域,所有静态数据都有其默认值。您需要一些持久性存储:数据库、文件、浏览器缓存等。您需要在后端持久化该数字。仅将其作为该类的静态字段是不够的。重新启动将重置它。(+丹尼斯说的…)怎么做@Dennis@manusharma:如何做什么?你应该更具体一些。怎么做?首先创建另一个静态对象(
code
static readony object myLockObject=new object();),然后用它序列化访问变量(code
lock(myLockObject){myStaticVariable++;})我已经按照你说的创建了一个类并调用GetMailCount()函数在aspx页面中,但它始终显示一个,但它不工作。每次它再次从0递增到1@manusharma你只是默默无闻。。告诉我你是如何实现这个类的,除非我不知道你是如何实现的,否则我无法解决它。你在aspx页面中写了什么?@manusharma只是一个按钮点击事件而已。。和一个单独的类文件Mail.cs