如何在服务器上发送电子邮件时在客户端上显示已发送电子邮件的计数

本文关键字:电子邮件 显示 客户端 服务器 | 更新日期: 2023-09-27 18:37:27

如何使用 ASP.NET(C#)在服务器上发送电子邮件时在客户端上显示已发送电子邮件的计数?

所以让我们看看我的场景:

  • 我在更新面板中有一个按钮。
  • 此按钮应向我的用户(服务器端)发送一些电子邮件。
  • 当我点击那个按钮时,我也想做一些这样的事情:

    1. 显示具有低不透明度的模态div,标签位于它(该标签的文本为"0")。
    2. 开始发送邮件。
    3. 该标签的文本应更新以反映每封已发送的电子邮件(这是一个计数器)(0->1->2->...
    4. 在发送邮件结束时,我想隐藏该模态div

和我的代码:

.ASPX:

<asp:UpdateProgress ID="UpdateProgress1" runat="server">
  <ProgressTemplate>
    <asp:Image ID="imgAjax" runat="server" ImageUrl="~/Images/ajax-loader.gif" />
  </ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
    <div id="RadbtnSendEmails_Container">
      <telerik:RadButton ID="RadbtnSendEmails" runat="server" EnableEmbeddedSkins="False"
        Skin="BlackByMe" Text="Send Mails" Width="150px" OnClick="RadbtnSendEmails_Click"
        Font-Bold="True" Font-Names="Tahoma" ValidationGroup="A">
      </telerik:RadButton>
    </div>
  </ContentTemplate>
</asp:UpdatePanel>
<div id="OffDiv">
</div>
<div id="CounterContainer">
  <asp:Image ID="imgWhiteCircle" runat="server" ImageUrl="~/Images/WhiteCircle.png" />
  <asp:Label ID="lblCounter" runat="server" Text="0" Font-Bold="True"
    Font-Size="50px" ForeColor="#6600CC">
  </asp:Label>
</div>

C

protected void RadbtnSendEmails_Click(object sender, EventArgs e) {
  ScriptManager.RegisterStartupScript(this, this.GetType(), "Show_Counter", 
    "Show_Counter();", true);
  //int Counter=0;
  //Send Mail Codes in for()
  //Counter++; in every send
  //Change that label'text to new counter -> i do n't know how can i do that
  //ScriptManager.RegisterStartupScript(this, this.GetType(), "Hide_Counter", 
    "Hide_Counter();", true);
}

但是,此 C# 代码存在问题:

当我单击该按钮时,UpdateProgress 中的图像会显示出来,在此块结束发送电子邮件后,我们可以看到模态div,但不是在过程开始时!

另外,我不知道如何实现该计数器!我应该从客户端代码跳转到服务器端代码还是从服务器端代码跳转到客户端代码。

这些电子邮件地址位于 Sql Server 2008R2 数据库中。所以我必须运行进程服务器端的那部分。

真的很想学习如何解决这个问题,我认为这种情况很好,所以请帮助我对此有一个好主意。

如何在服务器上发送电子邮件时在客户端上显示已发送电子邮件的计数

所以你面临的问题是你需要做一些工作服务器端,但你想显示那个客户端。问题的第一部分是HTTP是一种无状态的无连接协议,因此您必须不断向服务器询问更多信息(更新)。

因此,您需要的是几种资源:

一种让服务器知道当前发送的电子邮件数量的方法(例如您可以访问的公共计数器),服务器运行以发送电子邮件的方法(这通常需要在后台线程上运行,但要小心,崩溃的后台线程 - 例如来自未处理的异常 - 将折叠整个应用程序池)和一种从客户端初始化进程的方法(您已经有这部分)。

然后在客户端上,您需要一种启动请求的方法,一种检查更新并知道何时完成的方法,以及管理您想要的所有其他事情的方法。

我假设你正在使用jQuery(这是一个基于javascript的库,但不能取代javascript,这似乎是你需要学习的东西)并在这里编写一些基本代码。我还假设您在页面上运行了Microsoft ASP.NET 库(您的代码引用了ScriptManager,因此这是一个非常安全的选择)。

让我们从 C# 开始:(这在您的页面中,我不会写所有内容,只是它的大部分)

//this is a class you need to write, see below
//this way other code can access this variable too
public MySpecialMailerClass mailer;
[WebMethod] //this may require a include, you're on your own for that 
            //for now (System.Web.Services)
public void StartEmails(){
  //do some stuff here to start the email process?
  //create a class that can run the db scripts uninterrupted, 
  //and provide it a public member that can be accessed for the current
  //count, and the expected max count (set to -1 on init so you know 
  //that you haven't finished initting this yet)
  
  mailer = new MySpecialMailerClass();
  new Thread( mailer.Start ) { IsBackground = true }.Start( );
}
[WebMethod]
public int GetMaxCount(){
  return mailer.MaxCount;
}
[WebMethod]
public int GetCurrentCount(){
  return mailer.CurrentCount;
}

这实际上是您现在需要添加的所有 C# 代码。

Javascript:

var checkingOnEmailsSentIntervals,
    checkingOnMaxMessagesIntervals,
    maxMessages = 0;
function SuccessHandler(){
  //Note that you should do something here to handle the success each
  //time or that you should write a separate "SuccessHandler" 
  // for each one that you want to execute _when the server call is done_
  // Note that this is what I do (one success per call)
  // But in this case a generic one is sometimes handy, such as here
}
function FailHandler() {
  //Note that you should do something here to handle the failure each time
}
function startScript(){
  PageMethods.StartEmails(StartEmailsSuccessHandler,FailHandler);
}
function StartEmailsSuccessHandler(){
  //this will get triggered when the emails have started sending, 
  // so this is where you create the boxes you wanted earlier, etc.
  $('#counter-span-identifier').show();
  checkingOnEmailsSentIntervals = setInterval(CheckForMoreMessages,100);
  checkingOnMaxMessagesIntervals = setInterval(CheckForMaxMessages,10);
}
function CheckForMoreMessages() {
  PageMethods.GetCurrentCount(CheckForMoreMessagesSuccessHandler,FailHandler);
}
function CheckForMoreMessagesSuccessHandler(result) {
  var currentCount = result;
  $('#counter-span-identifier').text(currentCount + ' messages sent');
  if (currentCount == maxMessages) {
    //we have finished now, so we can display another message or hide our div
    clearInterval(checkingOnEmailsSentIntervals);
    $('#counter-span-identifier').hide();
  }
}
function CheckForMaxMessages() {
  PageMethods.GetMaxCount(CheckForMaxMessagesSuccessHandler,FailHandler);
}
function CheckForMaxMessagesSuccessHandler(result) {
  maxMessages = parseInt(result); //parseInt not really needed here, 
                                  //shown as example of coercion
  if (maxMessages > -1){
    clearInterval(checkingOnMaxMessagesIntervals);
  }
}

这应该非常巧妙地总结这里的情况,因为我们在javascript中有两个循环(setInterval)来检查MaxMessages(因此返回-1)和CurrentCount。我们还有代码,一旦每个循环获得所需的信息,就会停止它。

留给您的部分是将发送电子邮件的代码放入一个单独的类中,该类公开成员变量和一个方法(并重命名我的示例以使其符合您的要求)。

因为我认为需要完成它,所以这里是 C#,供MySpecialMailerClass开始:

public class MySpecialMailerClass {
  public MySpecialMailerClass() {
    MaxCount = -1;
    CurrentCount = 0;
  }
  public int MaxCount {get;set;}
  public int CurrentCount {get;set;}
  public void Start(){
    //do something to get the MaxCount from the database
    //do something to get the messages
    var messages = mailerDbClass.GetMessagesToBeSent();
    MaxCount = messages.Count;
    
    //send the messages
    foreach(message in messages) {
      SendMessage(message);
      CurrentCount += 1;
    }
  }
}

您需要了解浏览器不能直接执行服务器代码。服务器也不能直接在浏览器中执行代码。 最好的办法是以某种方式(Web服务)通过HTTP公开您希望从浏览器执行的方法。 然后使用 javascript 和 jQuery 调用它们,并以您描述的方式更改 UI。

当您在客户端上单击按钮时,将向服务器发送请求。 如果您使用的是更新面板,则此请求将使用 ajax 发送。 服务器收到请求后,它将运行按钮单击事件处理程序中的代码。 在单击处理程序中的代码完成之前,它不会将响应发送回客户端以更新页面。

您正在尝试做的事情(至少是客户端的计数器)仅使用更新面板几乎是不可能的。 如果你能克服没有那个,我会说跳过它。

我能想到的一种可能性是使用Web服务(.asmx)和ajax调用与自定义javascript。 您可以将每个单独的电子邮件请求发送到 Web 服务,当服务响应时,使用计数更新您的模式,然后将下一个请求发送到 Web 服务,依此类推。

相关文章: