1

I have an upload controller

        // Multiple files upload
        [HttpPost("upload/multiple")]
        public IActionResult Multiple(IFormFile[] files)
        {
            try
            {
                // Put your code here
                return StatusCode(200);
            }
            catch (Exception ex)
            {
                return StatusCode(500, ex.Message);
            }
        }

I am trying to use the IFormFile to get files for email attachments using MailKit

This is my email code

        private async Task SendEmail()
        {
            try
            {
                // create email message
                var email = new MimeMessage();
                email.From.Add(MailboxAddress.Parse(sender));
                email.To.Add(MailboxAddress.Parse(receiver));
                email.Subject = emailsubject;
                var multipart = new Multipart("mixed");
                multipart.Add(new TextPart(TextFormat.Html) { Text = emailMessage });
                foreach (var attachment in files)
                {
                    var content = new MemoryStream();
                    attachment.CopyTo(content);
                    content.Position = 0;

                    var contentType = ContentType.Parse(attachment.ContentType);
                    var part = new MimePart(contentType.MimeType)
                    {
                        FileName = Path.GetFileName(attachment.FileName),
                        ContentTransferEncoding = ContentEncoding.Base64,
                        Content = new MimeContent(content),
                    };

                    multipart.Add(part);
                }

                email.Body = multipart;
                //email.Body = new TextPart(TextFormat.Html) { Text = emailMessage};
                // send email
                using var smtp = new SmtpClient();
                smtp.Connect(outgoingServer, outgoingPort, SecureSocketOptions.Auto);
                smtp.Authenticate(userName, userPassword);
                smtp.Send(email);
                smtp.Disconnect(true);
            }
            catch (Exception ex)
            {
                NotificationService.Notify(NotificationSeverity.Error, "Send Email Error!", ex.Message, 7000);
            }
        }
    }

This generates an error on the 'files' variable "The name 'files' does not exist in the current content.

Can someone tell me what I am missing to pull the information from the controller for 'files'???

1 Answer 1

2

Here, in the Async Task SendEmail task, we have to set the parameter of type IFormFile like below,

private async Task sendEmail(IFormFile file)

Here is the Controller Code,

[HttpPost("send")]
    public async Task<IActionResult> SendMail(IFormFile file)
    {
        try
        {
            await mailService.sendEmailAsync(file);
            return Ok("Mail Sent!");
        }
        catch (Exception ex)
        {
            throw;
        }
            
    }

And We can't get IFormFile[] directly and pass it to the sendEmail method as a parameter. Instead we can create the model class with public List<IFormFile>? attachments{get;set;} and send the instance of the model class as a parameter to the sendEmail method.

2
  • Thanks for the comment! I have a couple questions...I was pretty sure I needed to add IFormFile file parameter to SendMail, but I am assuming your telling me I need a separate controller for the SendEmail (something like EmailController...) In your controller code, you are using mailService.SendEmailAsync(file) which give me an error ,,, the name "mailService" does not exist in the current context. Can you give me some more info on that part? (sorry edit timed out) Jul 5, 2022 at 17:43
  • 1
    mailService.SendEmailAsync(file). Here mailService is the class in which I have written my SendEmail method. Create a constructor in controller class and initialize the instance of the class in which you have been written your SendEmail method.
    – Deebikaa R
    Jul 6, 2022 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.