Class EmailService
- Namespace
- Builvero.Infrastructure.Services
- Assembly
- Builvero.Infrastructure.dll
Implementation of IEmailService using SendGrid for sending email verification codes.
public class EmailService : IEmailService
- Inheritance
-
EmailService
- Implements
- Inherited Members
Remarks
This service sends email verification codes using SendGrid's API. The API key is read from
configuration (Email:SendGridApiKey). If the API key is not configured, a warning is logged
and the email is not sent (allows the application to continue in development/test environments).
Constructors
EmailService(IConfiguration, IOptions<SendGridOptions>, ILogger<EmailService>)
Initializes a new instance of the EmailService class.
public EmailService(IConfiguration configuration, IOptions<SendGridOptions> sendGridOptions, ILogger<EmailService> logger)
Parameters
configurationIConfigurationApplication configuration for accessing SendGrid API key and email settings.
sendGridOptionsIOptions<SendGridOptions>SendGrid options including template IDs and sender metadata.
loggerILogger<EmailService>Logger for recording email operations and errors.
Methods
SendVerificationCodeAsync(string, string, string, string, CancellationToken)
Sends an email verification code to a user's email address using SendGrid.
public Task SendVerificationCodeAsync(string email, string code, string fullName, string verifyUrl, CancellationToken cancellationToken = default)
Parameters
emailstringThe recipient email address. Must be a valid email format.
codestringThe 6-digit verification code to send.
fullNamestringRecipient full name for personalization.
verifyUrlstringVerification URL to embed in the template.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task
- Implementation of using SendGrid for sending email verification codes.
Remarks
The email is sent using SendGrid's CreateSingleEmail(EmailAddress, EmailAddress, string, string, string) method with:
- From address: Configured via
Email:FromEmail(defaults to "noreply@builvero.com") - Subject: "Verify your Builvero account"
- HTML content: Includes welcome message and verification code with expiration notice
- Plain text content: Simple text version with the verification code
If the SendGrid API key is not configured (Email:SendGridApiKey), a warning is logged
and the method returns without sending the email. This allows the application to continue in
development/test environments where email may not be configured.
Exceptions
- Exception
Thrown when SendGrid API returns a non-success status code.