0

I am currently trying to get the files received from InputFile and attach it as an email attachment. I followed this website to get my InputFile with the progress bar: https://www.meziantou.net/file-upload-with-progress-bar-in-blazor.htm.

I have tried various options such as converting the file to byte array, using memory stream, and using a file stream but I do not have a path to copy the file too. Here's my code currently on what I am trying to accomplish. The Email is sent through SMTP client and that works perfectly without the attachments.

private async ValueTask LoadFiles(InputFileChangeEventArgs e)
{
    var files = e.GetMultipleFiles(maximumFileCount: 100);
    filesList = e.GetMultipleFiles(maximumFileCount: 100);


    var startIndex = uploadedFiles.Count;

    // Add all files to the UI
    foreach (var file in files)
    {
        var progress = new FileUploadProgress(file.Name, file.Size);
        uploadedFiles.Add(progress);
    }

    await using var timer = new Timer(_ => InvokeAsync(() => StateHasChanged()));
    timer.Change(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500));

    // Upload files
    byte[] buffer = System.Buffers.ArrayPool<byte>.Shared.Rent(4096);
    try
    {
        foreach (var file in files)
        {

            MemoryStream ms = new MemoryStream();

            using var stream = file.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024);
            while (await stream.ReadAsync(buffer) is int read && read > 0)
            {
                uploadedFiles[startIndex].UploadedBytes += read;
               

                file.OpenReadStream().CopyTo(ms);
                var fileBytes = ms.ToArray();

                Attachment fileAttch = new Attachment(new MemoryStream(fileBytes), file.ContentType);
                message.Attachments.Add(fileAttch);


                
                var readData = buffer.AsMemory().Slice(0, read);
            }

            startIndex++;
        }
    }
    finally
    {
        System.Buffers.ArrayPool<byte>.Shared.Return(buffer);

        // Update the UI with the final progress
        StateHasChanged();
    }
}

When using the debugger, I noticed that the try block breaks whenever I try to copy the file into MemoryStream. I am not sure why. Any help or solutions would be greatly appreciated.

Thank you

I have tried copying the file/buffer into the memory stream but the try block breaks. I have tried to use file stream without success. I am either missing something I am unaware of or I am not implementing the code correctly.

Trent
  • 1
  • 1
  • you need to split your task, first try to send a hello world email to yourself. – buga Dec 13 '22 at 16:21
  • 1
    You should post your code in a code block instead of an image. – Lex Dec 13 '22 at 16:27
  • I just added the code block. My apologies. Again, as mentioned above. The Smtp client works perfectly. I am able to send emails. I am just trying to add attachments – Trent Dec 13 '22 at 18:52

0 Answers0