C#C
C#3y ago
13p14

Sending File and Data to a Minimal API endpoint

Hi. I am using .NET 7, Minimal API and Mailkit.
What I want to do is to send an attachment along with other information in an email. I want to receive both the information and the file using a Form-Data request type but I am having problems receiving the information .
The error I am getting is:
IFromFormMetadata is only supported for parameters of type IFormFileCollection and IFormFile.
.

Here is the endpoint code :
app.MapPost("/sendEmailWithAttachments", async ([FromForm] REmailWithAttachments sendFileRequest) =>
{
    BSendEmail.sendEmailWithAttachments(EmailData: sendFileRequest, sendFileRequest.file.Name, sendFileRequest.file);
    return Results.Ok("Email sent successfully");
})

And here is the REmailWithAttachments class:
 public class REmailWithAttachments
    {
        public REmailWithAttachments(string subject, string toName, string toEmail, List<string> ccToEmail, IFormFile file)
        {
Subject = subject;
            ToName = toName;
            ToEmail = toEmail;
            CcToEmails = ccToEmail;
            this.file = file;
        }

        [Required]
        public string Subject { get; init; }
        [Required] public string ToName { get; init; }
        public string ToName { get; init; }
        [Required, EmailAddress]
        public string ParaEmail { get; init; }
        [Required]
        public List<string> CcToEmails { get; init; }
        [Mandatory] public IFormFile file { get; init; }
        public IFormFile file { get; init; }
    }
Was this page helpful?