Skip to content

Instantly share code, notes, and snippets.

@kasuken
Created September 11, 2023 14:32
Show Gist options
  • Save kasuken/fd5261904882d0525ceaa3cd10a3aa59 to your computer and use it in GitHub Desktop.
Save kasuken/fd5261904882d0525ceaa3cd10a3aa59 to your computer and use it in GitHub Desktop.
BlazorContactForm
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"EmailSettings": {
"ConnectionString": "endpoint=https://<your-resource-name>.communication.azure.com/;accesskey=<your-access-key>",
"FromAddress": "<your-email-address>"
},
"AllowedHosts": "*"
}
using System.ComponentModel.DataAnnotations;
namespace BlazorContactForm.Shared
{
public class ContactFormModel
{
[Required(ErrorMessage = "Please enter your name.")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter your email address.")]
[EmailAddress(ErrorMessage = "Please enter a valid email address.")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter a subject.")]
public string Subject { get; set; }
[Required(ErrorMessage = "Please enter a message.")]
public string Message { get; set; }
}
}
using Azure.Communication.Email;
namespace BlazorContactForm
{
public interface IEmailService
{
Task SendEmailAsync(string from, string to, string subject, string message);
}
public class EmailService : IEmailService
{
private readonly EmailClient emailClient;
public EmailService(EmailClient emailClient)
{
this.emailClient = emailClient;
}
public async Task SendEmailAsync(string from, string to, string subject, string message)
{
var fromEmailAddress = new EmailAddress(from);
var toEmailAddress = new EmailAddress(to);
await emailClient.SendAsync(Azure.WaitUntil.Started, fromEmailAddress.Address, toEmailAddress.Address, subject, message);
}
}
}
@page "/"
@inject IEmailService EmailService
<PageTitle>Index</PageTitle>
@using Microsoft.AspNetCore.Components.Forms
<h1>Contact Us</h1>
<EditForm Model="@contactFormModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="name">Name</label>
<InputText id="name" class="form-control" @bind-Value="@contactFormModel.Name" />
<ValidationMessage For="@(() => contactFormModel.Name)" />
</div>
<div class="form-group">
<label for="email">Email</label>
<InputText id="email" class="form-control" @bind-Value="@contactFormModel.Email" />
<ValidationMessage For="@(() => contactFormModel.Email)" />
</div>
<div class="form-group">
<label for="subject">Subject</label>
<InputText id="subject" class="form-control" @bind-Value="@contactFormModel.Subject" />
<ValidationMessage For="@(() => contactFormModel.Subject)" />
</div>
<div class="form-group">
<label for="message">Message</label>
<InputTextArea id="message" class="form-control" @bind-Value="@contactFormModel.Message" />
<ValidationMessage For="@(() => contactFormModel.Message)" />
</div>
<div class="alert @message?.StartsWith("Error") ? " alert-danger" : "alert-success" @hidden="@string.IsNullOrEmpty(message)">
@message
</div>
<button type="submit" class="btn btn-primary">Send</button>
</EditForm>
@code {
private ContactFormModel contactFormModel = new();
private string toAddress = "<your-email-address>";
private string message;
private async Task HandleValidSubmit()
{
try
{
await EmailService.SendEmailAsync(contactFormModel.Email, toAddress, contactFormModel.Subject, contactFormModel.Message);
message = "Your email has been sent successfully.";
await ClearMessage();
}
catch (Exception ex)
{
message = $"Error: {ex.Message}";
await ClearMessage();
}
}
private async Task ClearMessage()
{
await Task.Delay(3000);
message = null;
StateHasChanged();
}
}
builder.Services.AddSingleton<EmailClient>(sp =>
{
var configuration = sp.GetRequiredService<IConfiguration>();
var emailSettings = configuration.GetSection("EmailSettings");
var endpoint = emailSettings["ConnectionString"];
var connectionString = endpoint;
return new EmailClient(connectionString);
});
builder.Services.AddScoped<IEmailService, EmailService>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment