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

前端 未结 6 977
深忆病人
深忆病人 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: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.

提交回复
热议问题