In modern enterprises, users receive hundreds of emails daily, making manual categorization slow and error-prone. AI-powered email categorization automates sorting emails into categories like Important, Promotions, Support, or Spam, improving productivity and user experience.
This article explains how to build an AI-powered email categorization system using SQL Server for storage, ASP.NET Core backend, and optional Angular frontend, along with real-world best practices.
1. Understanding Email Categorization
Email categorization is the process of classifying incoming emails into predefined categories. AI models analyze:
Benefits
2. Architecture Overview
Email Client / Web Frontend
|
| HTTP / REST API
v
ASP.NET Core Web API
|
| ML Engine / AI Service
v
SQL Server Database
Frontend:displays categorized emails, allows manual corrections
Backend: fetches emails, interacts with AI model, stores categories
SQL Server: stores raw emails, metadata, and categorized results
AI Engine: predicts category for each email using machine learning or cloud AI
3. Database Design
3.1 Tables
Emails – stores incoming email data
Categories – predefined categories (Important, Promotions, Spam, etc.)
EmailCategories – stores predicted or manually corrected categories
Example: Emails Table
CREATE TABLE Emails (
EmailId INT PRIMARY KEY IDENTITY,
UserId INT NOT NULL,
Sender NVARCHAR(255) NOT NULL,
Subject NVARCHAR(500),
Body NVARCHAR(MAX),
ReceivedAt DATETIME2 DEFAULT GETDATE(),
FOREIGN KEY (UserId) REFERENCES Users(UserId)
);
EmailCategories Table
CREATE TABLE EmailCategories (
EmailCategoryId INT PRIMARY KEY IDENTITY,
EmailId INT NOT NULL,
CategoryId INT NOT NULL,
PredictedByAI BIT DEFAULT 1,
AssignedAt DATETIME2 DEFAULT GETDATE(),
FOREIGN KEY (EmailId) REFERENCES Emails(EmailId),
FOREIGN KEY (CategoryId) REFERENCES Categories(CategoryId)
);
4. ASP.NET Core Backend Implementation
4.1 Email Ingestion Endpoint
[HttpPost("ingest")]
public async Task<IActionResult> IngestEmail([FromBody] EmailDto email)
{
var newEmail = new Email
{
UserId = email.UserId,
Sender = email.Sender,
Subject = email.Subject,
Body = email.Body,
ReceivedAt = DateTime.UtcNow
};
_dbContext.Emails.Add(newEmail);
await _dbContext.SaveChangesAsync();
// Trigger AI categorization
await _emailCategorizationService.CategorizeEmailAsync(newEmail);
return Ok(new { message = "Email ingested and queued for categorization." });
}
4.2 AI Categorization Service
public class EmailCategorizationService : IEmailCategorizationService
{
private readonly IAiService _aiService;
private readonly AppDbContext _dbContext;
public EmailCategorizationService(IAiService aiService, AppDbContext dbContext)
{
_aiService = aiService;
_dbContext = dbContext;
}
public async Task CategorizeEmailAsync(Email email)
{
var category = await _aiService.PredictCategoryAsync(email.Subject, email.Body);
var emailCategory = new EmailCategory
{
EmailId = email.EmailId,
CategoryId = category.Id,
PredictedByAI = true,
AssignedAt = DateTime.UtcNow
};
_dbContext.EmailCategories.Add(emailCategory);
await _dbContext.SaveChangesAsync();
}
}
4.3 AI Model Approaches
Rule-Based Classification – keyword matching for simple scenarios
ML Models – supervised classification using Naive Bayes, SVM, or deep learning
Example using ML.NET
public class EmailData
{
public string Subject { get; set; }
public string Body { get; set; }
public string Category { get; set; }
}
public class EmailPrediction
{
[ColumnName("PredictedLabel")]
public string PredictedCategory { get; set; }
}
Train model using historical emails
Features: Subject + Body concatenation, sender metadata, and text preprocessing
Predict category and store in SQL Server
5. Angular Frontend Implementation
5.1 Display Emails with Categories
<table mat-table [dataSource]="emails" class="mat-elevation-z8">
<ng-container matColumnDef="sender">
<th mat-header-cell *matHeaderCellDef> Sender </th>
<td mat-cell *matCellDef="let email"> {{email.sender}} </td>
</ng-container>
<ng-container matColumnDef="subject">
<th mat-header-cell *matHeaderCellDef> Subject </th>
<td mat-cell *matCellDef="let email"> {{email.subject}} </td>
</ng-container>
<ng-container matColumnDef="category">
<th mat-header-cell *matHeaderCellDef> Category </th>
<td mat-cell *matCellDef="let email"> {{email.category}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
5.2 Manual Category Correction
updateCategory(emailId: number, categoryId: number) {
this.http.post(`/api/emails/update-category`, { emailId, categoryId })
.subscribe(() => this.loadEmails());
}
6. Performance Considerations
Batch Categorization – categorize emails in batches during off-peak hours
Cache frequently predicted categories to reduce repeated AI calls
Index Subject and Body columns for fast retrieval
Use full-text search to preprocess text for AI
7. Security Considerations
Sanitize email content before sending to AI service
Encrypt sensitive data in SQL Server
Authenticate API endpoints with JWT/OAuth2
Implement access control to prevent unauthorized category modifications
8. Real-world Best Practices
Incremental Model Updates: retrain models with user-corrected labels
Confidence Threshold: show AI suggestion only if confidence > 70%
Audit Trail: keep history of AI vs manual categorization
Scalable AI Deployment: use Azure Cognitive Services or cloud ML models for production
User Feedback Loop: improve AI accuracy based on corrections
9. Summary
AI-powered email categorization using SQL Server enables:
Implementation involves:
Storing email metadata in SQL Server
ASP.NET Core backend to ingest emails and call AI categorization service
AI models (ML.NET or cloud AI) to predict categories
Angular frontend to display categorized emails and allow manual corrections
Following performance, security, and monitoring best practices
With this approach, developers can build intelligent email systems that automate repetitive tasks and improve user productivity.