开发者

How do I avoid a delay when sending email from my application?

I have a small console application. It checks a few settings, makes some decisions, and sends an email. The problem is the email doesn't actually get sent until my application finishes. I want the email sent as soon as my method that sends the email completes.

Initially, I just created a MailMessage and called .Send(). That's when I noticed the mail was not being sent until the app finished.

Then I tried using the task parallel library.

var taskA = Task.Factory.StartNew(() => msg.Send());

Again, the messages don't get sent until my entire application finishes.

How 开发者_运维百科do I sent an email when msg.send executes, not when the app completes?


SmptClient supports async sending of mail via SendAsync, however in practice in a web application this hangs the request thread.

To avoid blocking I recommend using the ThreadPool to fire off the email in a background thread. This won't block your application.

ThreadPool.QueueUserWorkItem(o => {
    using (SmtpClient client = new SmtpClient(...))
    {
        using (MailMessage mailMessage = new MailMessage(...))
        {
            client.Send(mailMessage, Tuple.Create(client, mailMessage));
        }
    }
}); 


The most sure fire way to avoid delays would probably be to use a pickup directory, which will queue the message rather than send it immediately.


you should use a SMTP client. do it like this:

            MailMessage mm = new MailMessage();
            //fill in your message
            NetworkCredential nc = new NetworkCredential(FromAddress, FromPassword);
            SmtpClient sc = new SmtpClient(SmtpHost, SmtpPort);
            sc.EnableSsl = true;
            sc.Credentials = nc;
            sc.Send(mm);

at this stage your mail will be sent.

But, sending an email is an async act, so it will take some time until you recive the mail.


Create a new MailMessage and send it with SmtpClient. It will send immediately. I will add an example.

EDIT: Populate the variables host, port with the smtp ser ver name and port number.

using (var mailer = new SmtpClient(host, port))
{
    using (var message = new MailMessage(sender, recipient, subject, body) { IsBodyHtml = false })
    {
        mailer.UseDefaultCredentials = false;
        mailer.Credentials = new NetworkCredential(user, pass);
        mailer.EnableSsl = useSSL;
        mailer.Timeout = Timeout;
        mailer.Send(message);

    }
}

If you still experience a delay, then the delay will be at the mail server.


Simply dispose the MailMessage and SmtpClient objects after the .Send() function.

SmtpClient smtpClient = new SmtpClient("server", 25);
smtpClient.UseDefaultCredentials = true;

MailMessage message = new MailMessage("ToAddress","FromAddress");
message.Subject = "Test email";
message.Body = "Test email";

smtpClient.Send(message);

message.Dispose();
smtpClient.Dispose();


Use SmtpClient with setting:

smtpClient.ServicePoint.MaxIdleTime = 2;

https://weblogs.asp.net/stanleygu/tip-14-solve-smtpclient-issues-of-delayed-email-and-high-cpu-usage

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜