working on adding email to my app. Email sends fine but attachments are always zero. I never get into the onchange event. Send Help!
@rendermode InteractiveServer
@inject DialogService DialogService
@inject GraphEmailService EmailService
@inject IJSRuntime JSRuntime
@using NextGenDeacom.Services
@using NextGenDeacom.Models
@using Microsoft.JSInterop
<RadzenTemplateForm TItem="EmailModel" Data="@emailModel">
<RadzenFieldset Text="Compose Email">
<RadzenFormField Text="To Address" Style="width: 100%;">
<RadzenTextBox @bind-Value="emailModel.ToAddress" Style="width: 100%;" />
</RadzenFormField>
<RadzenFormField Text="CC Address" Style="width: 100%;">
<RadzenTextBox @bind-Value="emailModel.CCAddress" Style="width: 100%;" />
</RadzenFormField>
<RadzenFormField Text="Subject" Style="width: 100%;">
<RadzenTextBox @bind-Value="emailModel.Subject" Style="width: 100%;" />
</RadzenFormField>
<RadzenFormField Text="Body" Style="width: 100%;">
<RadzenHtmlEditor @bind-Value="emailModel.Body" Style="width: 100%; height: 450px;" />
</RadzenFormField>
<!-- File input for attachments -->
<RadzenFileInput @bind-Value=@fileData @bind-FileName=@fileName @bind-FileSize=@fileSize TValue="byte[]" Multiple="true"
Change=@(args => OnChange(args)) Error=@(args => OnError(args)) />
<RadzenButton Text="Send" ButtonStyle="ButtonStyle.Primary" Click="@SendEmail" />
<RadzenButton Text="Cancel" ButtonStyle="ButtonStyle.Secondary" Click="@(args => DialogService.Close())" />
</RadzenFieldset>
</RadzenTemplateForm>
@code {
private EmailModel emailModel = new EmailModel();
private byte[] fileData; // Holds the byte data of the selected file
private string fileName; // Holds the file name
private long? fileSize; // Holds the file size
void OnChange(byte[] value)
{
Console.WriteLine("File selected");
fileData = value;
if (fileData != null && fileData.Length > 0)
{
// Add the selected file to the attachments list
emailModel.Attachments.Add(new EmailAttachment
{
Name = fileName,
Content = fileData
});
Console.WriteLine($"Attachment added: {fileName}, Size: {fileSize}");
}
}
void OnError(UploadErrorEventArgs args)
{
Console.WriteLine($"File upload error: {args.Message}");
}
async Task SendEmail()
{
var email = new Email
{
ToEmail = emailModel.ToAddress,
CcEmail = emailModel.CCAddress,
Subject = emailModel.Subject,
Body = emailModel.Body,
Attachments = emailModel.Attachments,
FromEmail = "something@onmicrosoft.com" // Optional 'from' address
};
Console.WriteLine($"Sending email with {email.Attachments.Count} attachments");
await EmailService.SendEmailAsync(email);
DialogService.Close();
}
// Model to represent email data
public class EmailModel
{
public string ToAddress { get; set; }
public string CCAddress { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public List<EmailAttachment> Attachments { get; set; } = new List<EmailAttachment>();
}
}