2

コメント ボックスを追加し、ユーザーが新しいコメントを追加するたびに、Insert Handler を呼び出してこれを DB に適用します。ここで私が気に入っているのは、迅速な処理です。

私が今やりたかったことは、新しいコメントが挿入されるたびに遅延なく送信することです。

そのため、 SmtpClientを追加し、同じ Insert Handler でSendAsyncを使用してメールを送信しました。最も単純な電子メール本文 (「hello world」) でさえ、応答に 5 秒かかったので、それは私にとって十分ではありませんでした!!! 多分私は新しいスレッドを追加する必要がありますか?

メール送信の遅延を克服する他の方法はありますか? Insert Handler のonCompleteを実行し、バックグラウンドでメールの送信を行う別の Handler を呼び出す新しい Handler を作成することを考えましたが、ユーザーは気付かないでしょう。これに関する問題は、スパムである可能性がありますが、同じ Handler を何度も何度も呼び出すことです。

4

3 に答える 3

1

この問題に対処する方法は無数にありますが、私のアプローチは常に、データベースから直接読み取り、ASP.NETアプリケーションとは独立して電子メールを送信する完全に異なるタイミングのプロセスまたはサービスを使用することでした。

理想的な大規模アプリケーションでは、MSMQのような別のプロセスを使用します-受信側でのMicrosoftメッセージキューイング。

編集:

OK、それでいくつかの場所でオンラインで言及されている別のアプローチがあります(それは非常に壊れやすいので使用することを強くお勧めしません)が、基本的にそれは電子メールを送信するためのコードを含むASP.NETWebサービスを作成することを含みますjQueryを使用してそのサービスを呼び出す。したがって、最終的には、次のように宣言されたWebサービスになります。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class EmailSender : System.Web.Services.WebService
{
    [WebMethod]
    public void SendEmail(string email, string message)
    {
        //Send my email
    }
}

そして、提出またはフォームのポストバック時に、これらの線に沿って何かを呼び出します。

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8;",
    url: "EmailSender.asmx",
    data: JSON.stringify({ email: "mail@email.com",
        message: "this is a test message"
    }),
    dataType: "json",
    success: function (data, textStatus, jqXHR) {
        //Do success stuff
    },
    error: function (jqXHR, textStatus, errorThrown) {
        //Do error stuff
    }
});
于 2012-07-03T06:52:08.120 に答える
0

あるプロジェクトで同じ問題が発生しました。この問題に対する私の解決策は、送信する必要のある電子メールをダンプするために、ホームページのピックアップ場所で実行されている別の SMTP サーバーを用意することでした。これを行う場合、電子メールの作成は ASP .Net 側のインスタントから行われます。その後、SMTP サーバーが実際の送信手順を処理します。

これにより、Web ページの応答が速くなり、SMTP サーバーが他の SMTP サーバーによってブラックリストに登録されるほど多くの電子メールを一度に送信することがなくなりました。

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpdeliverymethod.aspx

于 2012-07-03T07:01:54.570 に答える
0

おそらく、このコード、SendMailMessageAsync() メソッドを使用できます。これは、BlogEngine のソース コードからのものです。

public static string SendMailMessage(MailMessage message)
{
    if (message == null)
    {
        throw new ArgumentNullException("message");
    }

    StringBuilder errorMsg = new StringBuilder();

    try
    {
        message.IsBodyHtml = true;
        message.BodyEncoding = Encoding.UTF8;
        var smtp = new SmtpClient(Settings.Instance.SmtpServer);

        // don't send credentials if a server doesn't require it,
        // linux smtp servers don't like that 
        if (!string.IsNullOrEmpty(Settings.Instance.SmtpUserName))
        {
            smtp.Credentials = new NetworkCredential(yourusername, yourpassword));
        }

        smtp.Port = Settings.Instance.SmtpServerPort;
        smtp.EnableSsl = Settings.Instance.EnableSsl;
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        errorMsg.Append("Error sending email in SendMailMessage: ");
        Exception current = ex;

        while (current != null)
        {
            if (errorMsg.Length > 0) { errorMsg.Append(" "); }
            errorMsg.Append(current.Message);
            current = current.InnerException;

            Logger.Error("Error sending email in SendMailMessage.", ex);
        }
    }
    finally
    {
        // Remove the pointer to the message object so the GC can close the thread.
        message.Dispose();
    }

    return errorMsg.ToString();
}

public static void SendMailMessageAsync(MailMessage message)
{
    ThreadPool.QueueUserWorkItem(delegate { SendMailMessage(message); });
}
于 2012-07-03T07:58:04.673 に答える