Skip to content

Commit 017456c

Browse files
Merge pull request #3 from hardikpatelse/new_enhancement_with_send_email
Implementation of Send Email With MailTrap Services
2 parents 54e633c + 117d8c5 commit 017456c

File tree

9 files changed

+137
-8
lines changed

9 files changed

+137
-8
lines changed

AIChatBot.API/AIChatBot.API.http

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1-
@AIChatBot.API_HostAddress = http://localhost:5103
1+
@AIChatBot.API_HostAddress = https://localhost:7154
22

3-
GET {{AIChatBot.API_HostAddress}}/weatherforecast/
4-
Accept: application/json
3+
4+
GET {{AIChatBot.API_HostAddress}}/api/tools/schema
5+
6+
###
7+
8+
POST {{AIChatBot.API_HostAddress}}/chat
9+
Content-Type: application/json
10+
11+
{
12+
//ChatRequest
13+
}
14+
15+
###
16+
17+
@modelId=string
18+
GET {{AIChatBot.API_HostAddress}}/chat/history?modelId={{modelId}}
19+
20+
###
21+
22+
GET {{AIChatBot.API_HostAddress}}/models
523

624
###
25+

AIChatBot.API/Data/chatHistory.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@
231231
"Role": "assistant",
232232
"Content": "\u2705 Tool \u0060CreateFile\u0060 executed:\n\u2705 File \u0027LeapYear.py\u0027 created.",
233233
"DateTime": "2025-07-02T16:29:34.704133Z"
234+
},
235+
{
236+
"Role": "user",
237+
"Content": "Send an email to hardikpatel.se@gmail.com with content for leave application from 7th July to 15th July.",
238+
"DateTime": "2025-07-06T12:16:31.1958001Z"
239+
},
240+
{
241+
"Role": "assistant",
242+
"Content": "\u2705 Tool \u0060SendEmail\u0060 executed:\n\uD83D\uDCE7 Email sent to hardikpatel.se@gmail.com with subject \u0027Leave Application\u0027.",
243+
"DateTime": "2025-07-06T12:16:44.9945279Z"
234244
}
235245
]
236246
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace AIChatBot.API.Models.Custom
2+
{
3+
public class MailData
4+
{
5+
public string EmailToId { get; set; }
6+
public string EmailToName { get; set; }
7+
public string EmailSubject { get; set; }
8+
public string EmailBody { get; set; }
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace AIChatBot.API.Models.Custom
2+
{
3+
public class MailSettings
4+
{
5+
public string EmailId { get; set; }
6+
public string Name { get; set; }
7+
public string UserName { get; set; }
8+
public string APIToken { get; set; }
9+
public string Host { get; set; }
10+
public int Port { get; set; }
11+
public bool UseSSL { get; set; }
12+
}
13+
}

AIChatBot.API/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using AIChatBot.API.Factory;
33
using AIChatBot.API.Interfaces;
44
using AIChatBot.API.Models;
5+
using AIChatBot.API.Models.Custom;
56
using AIChatBot.API.Services;
67
using Microsoft.OpenApi.Models; // Ensure this directive is present for Swagger support
78

@@ -23,9 +24,11 @@
2324
builder.Services.AddScoped<AgentService>();
2425
builder.Services.AddScoped<ToolsRegistryService>();
2526
builder.Services.AddScoped<IChatService, ChatService>();
27+
builder.Services.AddSingleton<MailService>();
2628

2729
builder.Services.Configure<OpenRouterModelsApi>(builder.Configuration.GetSection("OpenRouterModelsApi"));
2830
builder.Services.Configure<OllamaModelsApi>(builder.Configuration.GetSection("OllamaModelsApi"));
31+
builder.Services.Configure<MailSettings>(builder.Configuration.GetSection("MailSettings"));
2932

3033
builder.Services.AddScoped<ChatModelServiceFactory>();
3134
builder.Services.AddScoped<ChatHistoryService>();
@@ -41,6 +44,9 @@
4144

4245
var app = builder.Build();
4346

47+
// Set the root service provider for static access
48+
AIChatBot.API.Services.ServiceProviderAccessor.ServiceProvider = app.Services;
49+
4450
// Configure the HTTP request pipeline.
4551
if (app.Environment.IsDevelopment())
4652
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using AIChatBot.API.Models.Custom;
2+
using Microsoft.Extensions.Options;
3+
using System.Net;
4+
using System.Net.Mail;
5+
6+
namespace AIChatBot.API.Services
7+
{
8+
public class MailService
9+
{
10+
private readonly MailSettings _mailSettings;
11+
public MailService(IOptions<MailSettings> mailSettingsOptions)
12+
{
13+
_mailSettings = mailSettingsOptions.Value;
14+
}
15+
public bool SendMail(MailData mailData)
16+
{
17+
try
18+
{
19+
20+
var client = new SmtpClient(_mailSettings.Host, _mailSettings.Port)
21+
{
22+
Credentials = new NetworkCredential("api", _mailSettings.APIToken),
23+
EnableSsl = true
24+
};
25+
26+
client.Send(_mailSettings.EmailId, mailData.EmailToId, mailData.EmailSubject, mailData.EmailBody);
27+
28+
return true;
29+
}
30+
catch (Exception ex)
31+
{
32+
Console.WriteLine(ex.ToString());
33+
return false;
34+
}
35+
}
36+
}
37+
}

AIChatBot.API/Services/ToolFunctions.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace AIChatBot.API.Services
1+
using AIChatBot.API.Models.Custom;
2+
3+
namespace AIChatBot.API.Services
24
{
35
public static class ToolFunctions
46
{
@@ -20,9 +22,32 @@ public static string FetchWebData(string url)
2022

2123
public static string SendEmail(string to, string subject, string body)
2224
{
23-
// Simulate email sending (log to console)
24-
return $"📧 Email sent to {to} with subject '{subject}' \n Body: {body}.";
25+
// Build MailData
26+
var mailData = new MailData
27+
{
28+
EmailToId = to,
29+
EmailToName = to, // Or set as needed
30+
EmailSubject = subject,
31+
EmailBody = body
32+
};
33+
34+
// Get the service provider from a static accessor
35+
var provider = ServiceProviderAccessor.ServiceProvider;
36+
if (provider == null)
37+
return "❌ MailService not available.";
38+
39+
var mailService = provider.GetService<MailService>();
40+
if (mailService == null)
41+
return "❌ MailService not available.";
42+
43+
var result = mailService.SendMail(mailData);
44+
return result ? $"📧 Email sent to {to} with subject '{subject}'." : "❌ Failed to send email.";
2545
}
2646
}
2747

48+
// Static accessor for the root IServiceProvider
49+
public static class ServiceProviderAccessor
50+
{
51+
public static IServiceProvider ServiceProvider { get; set; }
52+
}
2853
}

AIChatBot.API/appsettings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,15 @@
1212
"OpenRouterModelsApi": {
1313
"Url": "https://openrouter.ai/api/v1/chat/completions",
1414
"ApiKey": "YOUR_KEY_HERE"
15+
},
16+
"MailSettings": {
17+
"Host": "live.smtp.mailtrap.io",
18+
"DefaultCredentials": false,
19+
"Port": 587,
20+
"Name": "Hardik Patel",
21+
"EmailId": "hello@demomailtrap.co",
22+
"UserName": "hello@demomailtrap.co",
23+
"APIToken": "MAILTRAP_API_TOKEN",
24+
"UseSSL": true
1525
}
1626
}

AIChatBot.UI/src/app/components/chat/chat.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { ChatService } from '../../services/chat.service'
33
import { Model } from '../../entities/model'
44
import { ChatHistoryResponse, ChatMessage } from '../../entities/chat-history'
55
import { marked } from 'marked'
6-
import { ORCall } from '../../services/orcall.service'
76

87
@Component({
98
selector: 'app-chat',
@@ -21,7 +20,7 @@ export class Chat implements OnInit {
2120
errorMessage: string = '';
2221
selectedChatMode: string = 'chat';
2322

24-
constructor(private chatService: ChatService, private cdr: ChangeDetectorRef, private orCall: ORCall) { }
23+
constructor(private chatService: ChatService, private cdr: ChangeDetectorRef) { }
2524

2625
ngOnInit(): void {
2726
this.chatService.getModels().subscribe({

0 commit comments

Comments
 (0)