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

前端 未结 6 912
深忆病人
深忆病人 2021-01-26 03:23

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

相关标签:
6条回答
  • 2021-01-26 04:01

    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.

    0 讨论(0)
  • 2021-01-26 04:02

    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.

    0 讨论(0)
  • 2021-01-26 04:10

    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));
            }
        }
    }); 
    
    0 讨论(0)
  • 2021-01-26 04:10

    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();
    
    0 讨论(0)
  • 2021-01-26 04:12

    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.

    0 讨论(0)
  • 2021-01-26 04:20

    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 讨论(0)
提交回复
热议问题