This project demonstrates an efficient email service using Hangfire for background job processing in .NET applications. The service integrates SMTP for sending emails, PostgreSQL for persistent data storage, and the Options pattern for configurable application settings.
- Email Sending via Hangfire: Leverage Hangfire to schedule and execute background email jobs.
- SMTP Integration: Easily configurable for any SMTP provider to send emails.
- PostgreSQL Database: Reliable data storage for job tracking and application state.
- Options Pattern: Simplifies configuration and keeps code maintainable.
- Scalable Job Processing: Add and manage background jobs effortlessly.
- .NET 6 or higher
- PostgreSQL database
- A valid SMTP configuration (e.g., Gmail, SendGrid, etc.)
- Clone the repository:
git clone https://github.com/MrEshboboyev/Hangfire.git
- Navigate to the project directory:
cd Hangfire
- Restore dependencies:
dotnet restore
- Configure the application settings in
appsettings.json
.
Update the appsettings.json
file with the necessary settings:
-
Database Connection:
"ConnectionStrings": { "PostgresConnectionString": "Host=localhost;Database=YourDatabaseName;Username=YourUsername;Password=YourPassword" }
-
SMTP Configuration:
"ServerOptions": { "Email": "youremail@example.com", "Password": "YourEmailPassword" }
-
Hangfire Dashboard (optional):
"Hangfire": { "DashboardPath": "/hangfire", "Username": "admin", "Password": "adminpassword" }
Run the application in development mode:
dotnet run --environment Development
Monitor and manage background jobs through the Hangfire dashboard. By default, the dashboard is available at:
http://localhost:5000/hangfire
Log in using the configured credentials in appsettings.json
.
To send an email through a Hangfire job, use the following code snippet in your application:
BackgroundJob.Enqueue(() => emailService.SendEmailAsync("recipient@example.com", "Password"));
To apply database migrations for PostgreSQL:
dotnet ef migrations add InitialCreate
dotnet ef database update
Contributions are welcome! To contribute:
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
. - Commit your changes:
git commit -m "Add feature"
. - Push to the branch:
git push origin feature-name
. - Open a pull request.
This project is licensed under the MIT License. See the LICENSE
file for more details.
Happy coding! 🚀