Skip to content

marwanfarook22/Email_notfiction_system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“§ Email Notification System

A flexible and extensible email notification system built with C# that demonstrates the Adapter and Factory design patterns. This project allows seamless integration with multiple email service providers through a unified interface.

.NET C# License


πŸ“‘ Table of Contents


🌟 Features

✨ Multiple Email Service Providers

  • SMTP - Traditional email protocol support
  • SendGrid - Cloud-based email delivery service
  • Gmail API - Google's email service integration
  • Mailgun - Transactional email API

🎯 Design Patterns Implemented

  • Adapter Pattern - Converts incompatible email service APIs into a common interface
  • Factory Pattern - Centralizes object creation for email services and notifications
  • Dependency Injection - Promotes loose coupling and testability

πŸ“¨ Pre-built Notification Templates

  • Welcome Email
  • Password Reset Email
  • Order Confirmation Email

πŸ”Œ Easy Provider Switching

Switch between email providers without changing your business logic - just change the factory selection!

βœ… Input Validation

  • Email format validation
  • User input validation
  • Connection testing before sending

πŸ—οΈ Project Structure

Email_notification_system/
β”œβ”€β”€ Models/
β”‚   β”œβ”€β”€ EmailMessage.cs          # Email data model
β”‚   └── EmailResult.cs           # Result object for email operations
β”œβ”€β”€ Interfaces/
β”‚   └── IEmailService.cs         # Common interface for all email services
β”œβ”€β”€ Third_Party_Services/
β”‚   β”œβ”€β”€ SmtpClient.cs            # SMTP client implementation
β”‚   β”œβ”€β”€ SendGridApiClient.cs     # SendGrid API client
β”‚   β”œβ”€β”€ GmailClient.cs           # Gmail API client
β”‚   └── MailgunClient.cs         # Mailgun API client
β”œβ”€β”€ Adaptors/
β”‚   β”œβ”€β”€ SmtpClient_Adaptor.cs    # SMTP adapter
β”‚   β”œβ”€β”€ SendGridApi_Adaptor.cs   # SendGrid adapter
β”‚   β”œβ”€β”€ GmailClientAdaptor.cs    # Gmail adapter
β”‚   └── MailgunClient_Adaptor.cs # Mailgun adapter
β”œβ”€β”€ Service/
β”‚   └── NotificationManager.cs   # High-level notification service
β”œβ”€β”€ Factories/
β”‚   β”œβ”€β”€ ThirdParty_service_Factory.cs      # Email service factory
β”‚   └── NotificationManager_Factory.cs     # Notification factory
└── Program.cs                   # Main application entry point

πŸš€ Getting Started

Prerequisites

  • .NET 8.0 SDK or higher
  • Visual Studio 2022 / VS Code / Rider

Installation

  1. Clone the repository
git clone https://github.com/yourusername/email-notification-system.git
cd email-notification-system
  1. Build the project
dotnet build
  1. Run the application
dotnet run

πŸ’» Usage

Running the Application

When you run the application, you'll see an interactive menu:

╔════════════════════════════════════════╗
β•‘     Email Notification System - Demo   β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Select Third-Party Email Service:
1. SMTP
2. SendGrid
3. Gmail
4. Mailgun
Please select an option (1-4):

Example Workflow

  1. Select Email Service Provider

    • Choose from SMTP, SendGrid, Gmail, or Mailgun
  2. Enter Email Details

    • Receiver Email
    • Sender Email
    • Subject
    • Body
  3. Choose Notification Type

    • Welcome Email
    • Password Reset Email
    • Order Confirmation Email
  4. Email Sent!

    • Confirmation message displayed

πŸ”§ How It Works

Adapter Pattern

Each email service has its own unique API. The Adapter pattern converts these different APIs into a single IEmailService interface:

public interface IEmailService
{
    EmailResult SendEmail(EmailMessage message);
    bool ValidateConnection();
}

Example Adapter:

public class SendGridApi_Adaptor : IEmailService
{
    private readonly SendGridApiClient _client;
    
    public SendGridApi_Adaptor(SendGridApiClient client)
    {
        _client = client;
    }
    
    public EmailResult SendEmail(EmailMessage message)
    {
        // Convert EmailMessage to SendGrid format
        var response = _client.Send(message.From, message.To, 
                                     message.Subject, message.Body);
        
        // Convert SendGrid response to EmailResult
        return new EmailResult
        {
            Success = response.StatusCode == 202,
            MessageId = response.MessageId
        };
    }
}

Factory Pattern

The Factory pattern centralizes object creation, making it easy to switch between providers:

IEmailService service = thirdPartyServiceFactory.GetThirdPartyService(userSelection, email);

Notification Manager

High-level service that uses any email provider through dependency injection:

var notificationManager = new NotificationManager(emailService);
notificationManager.SendWelcomeEmail("user@example.com", "John Doe");

πŸ“‹ Code Examples

Sending a Welcome Email with SendGrid

// Create email message
var emailMessage = new EmailMessage
{
    To = "newuser@example.com",
    From = "noreply@myapp.com",
    Subject = "Welcome!",
    Body = "Welcome to our platform!"
};

// Get SendGrid service from factory
IEmailService emailService = factory.GetThirdPartyService(2, emailMessage);

// Send using Notification Manager
var notificationManager = new NotificationManager(emailService);
notificationManager.SendWelcomeEmail("newuser@example.com", "John");

Switching Providers

// Use SMTP
IEmailService smtp = factory.GetThirdPartyService(1, emailMessage);

// Switch to Gmail - same interface!
IEmailService gmail = factory.GetThirdPartyService(3, emailMessage);

// Both work with NotificationManager
var manager1 = new NotificationManager(smtp);
var manager2 = new NotificationManager(gmail);

🎨 Design Patterns Deep Dive

Why Adapter Pattern?

Problem: Each email service has different APIs:

  • SendGrid: Send(from, to, subject, htmlContent)
  • Mailgun: SendMessage(MailgunMessage)
  • SMTP: Send(from, to, subject, body)

Solution: Create adapters that convert all APIs to a common interface IEmailService

Benefits:

  • βœ… Unified interface for all providers
  • βœ… Easy to add new providers
  • βœ… Business logic doesn't depend on specific APIs

Why Factory Pattern?

Problem: Creating email service objects requires knowing:

  • Which concrete class to instantiate
  • What parameters each service needs
  • How to configure each service

Solution: Factory handles all object creation logic

Benefits:

  • βœ… Centralized object creation
  • βœ… Hides complexity from client code
  • βœ… Easy to extend with new services

πŸ” Key Classes

EmailMessage

Data model representing an email:

public class EmailMessage
{
    public string To { get; set; }
    public string From { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

EmailResult

Result of an email operation:

public class EmailResult
{
    public bool Success { get; set; }
    public string MessageId { get; set; }
    public string ErrorMessage { get; set; }
}

IEmailService

Common interface for all email services:

public interface IEmailService
{
    EmailResult SendEmail(EmailMessage message);
    bool ValidateConnection();
}

πŸ› οΈ Extending the System

Adding a New Email Provider

  1. Create the third-party client
public class NewEmailClient
{
    public ResponseType Send(parameters) { }
}
  1. Create an adapter
public class NewEmailAdapter : IEmailService
{
    private readonly NewEmailClient _client;
    
    public EmailResult SendEmail(EmailMessage message)
    {
        // Adapt the interface
    }
}
  1. Add to factory
case 5: return CreateNewEmailAdapter(mail);

That's it! No changes needed in business logic.

🎯 Benefits of This Architecture

Benefit Description
Flexibility Switch providers without code changes
Maintainability Each provider isolated in its own adapter
Testability Easy to mock IEmailService interface
Scalability Add new providers without breaking existing code
Clean Code Clear separation of concerns

πŸ“š Learning Resources

This project demonstrates:

  • SOLID Principles

    • Single Responsibility Principle
    • Open/Closed Principle
    • Liskov Substitution Principle
    • Interface Segregation Principle
    • Dependency Inversion Principle
  • Design Patterns

    • Adapter Pattern
    • Factory Pattern
    • Dependency Injection

🀝 Contributing

Contributions are welcome! Here are some ideas:

  • Add more email providers (AWS SES, Postmark, etc.)
  • Implement unit tests with xUnit
  • Add email templates system
  • Implement retry logic for failed sends
  • Add logging functionality
  • Create a fallback mechanism

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

MarwanFarouq

πŸ™ Acknowledgments

  • Inspired by real-world email service integration challenges
  • Built to demonstrate design patterns in practice
  • Created as a learning project for software architecture

πŸ“ž Support

If you have any questions or run into issues, please open an issue on GitHub.


⭐ If you found this project helpful, please give it a star! ⭐

About

Simple app to generated Email nonfiction with kinds of third party system follow Adaptor design pattern and Factory method pattern

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages