This .NET project demonstrates how to use SendGrid to send emails in your application. SendGrid is a cloud-based email delivery service that allows you to send transactional and marketing emails.
Before you can use this project, make sure you have the following prerequisites:
- .NET 7.0 installed on your system.
- A SendGrid account. If you don't have one, you can sign up for a free SendGrid account at SendGrid Signup.
You will need to configure your SendGrid API key and email settings in the appsettings.json
file. Here is an example of the appsettings.json
file:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"SendGridEmailSettings": {
"ApiKey": "YOUR_SENDGRID_API_KEY",
"FromEmail": "your.email@example.com",
"FromName": "Your Name"
}
}
Replace "YOUR_SENDGRID_API_KEY"
with your SendGrid API key and set the "FromEmail" and "FromName" with your preferred values.
-
SendGridEmailService.cs: This class contains the logic to send emails using the SendGrid API. It utilizes the SendGrid C# library to send emails.
-
Controllers/EmailController.cs: This controller exposes an endpoint for sending emails. You can customize this endpoint as needed for your application.
This project uses the following NuGet packages, which are defined in the csproj
file:
-
Microsoft.AspNetCore.OpenApi
: This package provides OpenAPI (Swagger) integration for documenting your API. -
SendGrid
: This package is the SendGrid C# library for sending emails. -
SendGrid.Extensions.DependencyInjection
: This package provides integration with the SendGrid API via dependency injection. -
Swashbuckle.AspNetCore
: This package is used for Swagger documentation of the API.
To use SendGrid to send emails in your application, follow these steps:
-
Clone or download this project to your local machine.
-
Configure your
appsettings.json
file with your SendGrid API key and email settings as described in the "Configuration" section. -
Build and run the project using Visual Studio, Visual Studio Code, or the command line.
-
Use the
/api/email/send
endpoint to send emails through SendGrid.
You can use a tool like Postman or any API client to send POST requests to the /api/email/send
endpoint with the necessary email details in the request body.
Example POST request body:
{
"toEmail": "recipient@example.com",
"subject": "Hello from SendGrid",
"body": "This is a test email sent from SendGrid!"
}
If you'd like to contribute to this project or have any suggestions or improvements, feel free to submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.