Exemplo n.º 1
0
        public async Task ExecuteAsync(ISmtpService smtpService, AttachmentCollection attachments, EmailModel emailModel)
        {
            Email.DefaultSender   = smtpService.GetSmtpSender();
            Email.DefaultRenderer = new RazorRenderer();

            var maxMailSize = 25;

            if ((attachments.AttachmentSize / (1024 * 1024)) < maxMailSize)
            {
                try
                {
                    var email = await Email
                                .From(smtpService.SmtpSettings.FromAddress, "swatincadmin")
                                .To(emailModel.ToAddress, emailModel.ToDisplayName)
                                .Subject(emailModel.Subject)
                                .Attach(attachments.Attachments)
                                .UsingTemplate(emailModel.Template, emailModel.TemplateModel)
                                .SendAsync();

                    //close all filestreams
                    foreach (var item in attachments.Attachments)
                    {
                        item.Data.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                throw new Exception($"Cannot mail since the mail size exceeds maximum allowed {maxMailSize} MB");
            }
        }