lukencode / FluentEmail

All in one email sender for .NET. Supports popular senders (SendGrid, MailGun, etc) and Razor templates.
https://lukelowrey.com/dotnet-email-guide-2021/
MIT License
3.04k stars 438 forks source link

Could not find a part of the path 'C:\inetpub\AspNetCoreWebApps\app\EmailTemplates\example.cshtml' #277

Closed Iamkemical closed 3 years ago

Iamkemical commented 3 years ago

I'm sending emails using FluentEmail and Hangfire for background jobs. I get this error when i try sending emails. Could not find a part of the path 'C:\inetpub\AspNetCoreWebApps\app\EmailTemplates\example.cshtml'

From Hangfire I'm able to see that the job fails but after some retries the mails go through, this can take from 3 to 25 minutes sometimes. There are also times when the mails goes almost immediately. Here is my code

        public async Task<SendResponse> SendLoginMail(string recipientEmail, EmailLoginModel model)
        {
            try
            {
                using (var scope = serviceProvider.CreateScope())
                {
                    var mailer = scope.ServiceProvider.GetRequiredService<IFluentEmail>();
                    var email = mailer
                        .To(recipientEmail)
                        .Subject("Login Notification")
                        .UsingTemplateFromFile($"{Directory.GetCurrentDirectory()}/EmailTemplates/LoginTemplate.cshtml",
                        new
                        {
                            LoginDescription = model.LoginDescription,
                            IPAddress = model.IPAddress
                        });

                    return await email.SendAsync();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

Here's how I'm calling the the email service and enqueuing it using Hangfire

        [HttpPost("SendLoginMail")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AllowAnonymous]
        public IActionResult SendLoginMail([FromBody] EmailLoginModel model)
        {
            if (!ModelState.IsValid)
                return new ObjectResult(new NotificationResponse
                {
                    Status = "Error",
                    ResponseCode = 400,
                    Data = null,
                    ResponseMessage = ModelState
                });

            var jobId = backgroundJobClient.Enqueue(() => emailService
               .SendLoginMail(model.RecipientEmail, model));

            return new ObjectResult(new NotificationResponse
            {
                Status = "Success",
                ResponseCode = 200,
                Data = $"Job Id: {jobId}",
                ResponseMessage = "Processing Request..."
            });
        }

What could be the problem? Why does the method sometimes fail and goes through after 2 or more retries? @0xced @SimonCropp @mickeyr

Thijmen commented 2 years ago

How did you manage to solve this? @Iamkemical

Iamkemical commented 2 years ago

I discovered that the html email templates were not copied to the output directory. This I did by right clicking on the HTML file then selecting the property and setting it as copy if newer. This resolved the issue I was having. @Thijmen