バックグラウンドでメールを送信するには、SMTPClient.SendAsync を使用します。メール送信からのコールバックが完了したときにキャプチャするイベント ハンドラーを作成し、スピナーを非表示にします。
例えば
// Code to show your spinning circle goes here
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
...
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Code to hide your spinning circle goes here!
}
これは、javascript を使用して画像を表示し、コード ビハインドで非表示にする実際の例です。非同期で送信する必要はありません。(エラーハンドリング省略)
sendmail.aspx ページ:
<body>
<form id="form1" runat="server">
<div>
From: <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox><br />
To: <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</div>
<div id="spinner_div">
<asp:Image ID="imgSpinner" runat="server" ImageUrl="~/Images/ajax-loader.gif"></asp:Image>
</div>
<input type="button" onclick="document.getElementById('imgSpinner').style.display = ''; document.forms[0].submit();" value="Send Mail" />
</form>
</body>
sendmail.aspx.cs
public partial class Sendmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
imgSpinner.Attributes.Add("style", "display:none");
if (IsPostBack)
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Send(new System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text));
}
}
}