Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Send Async takes a long time #269


User avatar
0
naeazach created

I made a simple mailer like so: public class AppMailerService : DownPaymentAppServiceBase, IAppMailerService { private readonly IEmailSender _emailSender; private readonly ISmtpEmailSenderConfiguration _smtpConfig;

    public AppMailerService(ISmtpEmailSenderConfiguration smtpConfig, IEmailSender emailSender)
    {
        _smtpConfig = smtpConfig;
        _emailSender = emailSender;
    }


    public async Task SendSimpleMessageAsync(MailerInputDto input)
    {
        var smtpClient = new SmtpEmailSender(_smtpConfig).BuildClient();
        
        var from = new MailAddress(_smtpConfig.UserName);
        var msg = new MailMessage(from, new MailAddress(input.ToAddresses))
        {
            Subject = input.Subject,
            Body = input.Message,
            IsBodyHtml = true,

        };
        if (input.CCAddress != null)
        {
            msg.CC.Add(new MailAddress(input.CCAddress));
        }
        if (input.BCCAddress != null)
        {
            msg.Bcc.Add(new MailAddress(input.BCCAddress));
        }
        await _emailSender.SendAsync(msg, true);
       // smtpClient.Send(msg);
    }

}

I've noticed the await SendAsync seems to take a long time... 15 seconds-ish... is there a wait to skip the await method here? I don't care about server response since everything is being logged to database.

Thanks!


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    As I understand, your mail server response is slow and you don't want to wait it. If so, just start a new Task and call Send method in a try-cache and log exceptions if any. (Note that; to call SendAsync as sync, you can use AsyncHelper like AsyncHelper.RunSync(() => _emailSender.SendAsync(msg, true)) ) Please try, if you can not do it, I can help again.

    Have a nice day.