How do I avoid a delay when sending email from my

2019-03-06 07:43发布

问题:

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?

Thanks!

回答1:

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));
        }
    }
}); 


回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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();


回答6:

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



标签: c# .net-4.0