C
C#3y ago
Obel

Email with attachments

I am trying to add attachments to an email, but its always null In my mailDto i have IFormFile List to store the attachments, but from all my research in the frontend they use an IBrowserFile to show selected files. How can i send my IBrowserFile to my IFormFileList. I will attach screenshots for reference If you need any more information, please ask! Thanks!
23 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
Would SendGrid be a better option? SMTP was just the first i stumbled upon, seemed to have lots of documentation and guides I am using Mailkit btw, just using an instance of SMTPClient for configuration of mail settings
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
Indeed it is using MailKit.Net.Smtp;
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
But also someone suggested SendGrid when i first started this email thingf
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
My only problem is frontend, in swagger everything works fine, but i need to figure out how i can take the uploaded files, and send them into my MailDTO list of attachments..
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
In case you did not read the docs: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0 Client Code:
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
ServerCode:
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
The field's name in the body when doing the POST, needs to match the binded field in the Controler's Action https://media.discordapp.net/attachments/569261465463160900/1001188081069735936/unknown.png
Upload files in ASP.NET Core
How to use model binding and streaming to upload files in ASP.NET Core MVC.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
So you suggest uploading every file by itself, adding them 1 by one to the email?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
This is true
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
So i would create like an UploadResult model? to hold the data
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
Aight, its a bit more complicated than i anticipated with this email, i'll dig into it So actually my first step is to upload the attachment, and then worrying about sending an email with it after
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
My end goal is to be able to send automated emails based on some input, but just starting with manually sending an e-mail
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Obel
ObelOP3y ago
Yep, i thought so
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server