SmtpMailClient.SendAsync() 経由でのメール送信に問題があります。つまり、SmtpMailClient.SendAsync() の直後にアプリケーションを閉じると、メールが送信されません。
では、コールバックまでアプリを強制的に閉じないようにする方法は?
ありがとう !!
SmtpMailClient.SendAsync() 経由でのメール送信に問題があります。つまり、SmtpMailClient.SendAsync() の直後にアプリケーションを閉じると、メールが送信されません。
では、コールバックまでアプリを強制的に閉じないようにする方法は?
ありがとう !!
最後に、ブール変数を使用して、メールが送信されたかどうかを確認しました。
コード:
public static bool mailsent ;
// I am not posting the mail sending code as its available every where.
private void sendMailComplete(Object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MailMessage msd = e.UserState as MailMessage;
if (!e.Cancelled)
{
MessageBox.Show("Cancelled");
}
if (e.Error != null)
{
MessageBox.Show("Error");
}
else
{
mailsent = true;
}
}
FormClosing イベントで
private void MainForm_FormClosing(object sender, FormClosingEventArgs e){
if (!mailsent)
{
MessageBox.Show("Please wait, Mail Sending in Process !!! ");
e.Cancel = true;
}
}
それが役に立てば幸い !!
SmtpClient には SendCompleted イベントがあります。実装方法のサンプル コードは次のとおりです: http://www.systemnetmail.com/faq/4.6.aspx
プラットフォームによっては、保留中の SendAsync 操作をカウントし、それらが 0 の場合にのみアプリケーションを終了できるようにする、ある種の Application オブジェクトを実装する必要があります。
SendCompleted http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.sendcompleted.aspxイベント ハンドラーを SmtpClient オブジェクトに追加します。
問題は本当に次のとおりだと思います: アプリケーションの状態に基づいて、終了コマンドをどのようにインターセプトし、それを延期しますか?
WinForms の場合:
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (isSending)
{
quitOnSent = true; // make it so the quit will eventually happen
e.Cancel = true; // prevent the quit for now
}
base.OnFormClosing(e);
}
void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
{
if(quitOnSent) this.Close(); // now quit
}