|
1 | 1 | using BotSharp.Abstraction.Email.Settings; |
2 | 2 | using BotSharp.Plugin.EmailHandler.LlmContexts; |
| 3 | +using MailKit; |
3 | 4 | using MailKit.Net.Smtp; |
4 | 5 | using MailKit.Security; |
5 | 6 | using Microsoft.AspNetCore.Http; |
6 | 7 | using Microsoft.Extensions.Logging; |
7 | 8 | using MimeKit; |
8 | 9 | using System.Net.Http; |
9 | 10 |
|
10 | | -namespace BotSharp.Plugin.EmailHandler.Functions |
| 11 | +namespace BotSharp.Plugin.EmailHandler.Functions; |
| 12 | + |
| 13 | +public class HandleEmailRequestFn : IFunctionCallback |
11 | 14 | { |
12 | | - public class HandleEmailRequestFn : IFunctionCallback |
13 | | - { |
14 | | - public string Name => "handle_email_request"; |
15 | | - public string Indication => "Handling email request"; |
| 15 | + public string Name => "handle_email_request"; |
| 16 | + public string Indication => "Handling email request"; |
16 | 17 |
|
17 | | - private readonly IServiceProvider _services; |
18 | | - private readonly ILogger<HandleEmailRequestFn> _logger; |
19 | | - private readonly IHttpClientFactory _httpClientFactory; |
20 | | - private readonly IHttpContextAccessor _context; |
21 | | - private readonly BotSharpOptions _options; |
22 | | - private readonly EmailPluginSettings _emailSettings; |
| 18 | + private readonly IServiceProvider _services; |
| 19 | + private readonly ILogger<HandleEmailRequestFn> _logger; |
| 20 | + private readonly IHttpClientFactory _httpClientFactory; |
| 21 | + private readonly IHttpContextAccessor _context; |
| 22 | + private readonly BotSharpOptions _options; |
| 23 | + private readonly EmailHandlerSettings _emailSettings; |
23 | 24 |
|
24 | | - public HandleEmailRequestFn(IServiceProvider services, |
25 | | - ILogger<HandleEmailRequestFn> logger, |
26 | | - IHttpClientFactory httpClientFactory, |
27 | | - IHttpContextAccessor context, |
28 | | - BotSharpOptions options, |
29 | | - EmailPluginSettings emailPluginSettings) |
30 | | - { |
31 | | - _services = services; |
32 | | - _logger = logger; |
33 | | - _httpClientFactory = httpClientFactory; |
34 | | - _context = context; |
35 | | - _options = options; |
36 | | - _emailSettings = emailPluginSettings; |
37 | | - } |
38 | | - public async Task<bool> Execute(RoleDialogModel message) |
39 | | - { |
40 | | - var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions); |
41 | | - var recipient = args?.ToAddress; |
42 | | - var body = args?.Content; |
43 | | - var subject = args?.Subject; |
| 25 | + public HandleEmailRequestFn(IServiceProvider services, |
| 26 | + ILogger<HandleEmailRequestFn> logger, |
| 27 | + IHttpClientFactory httpClientFactory, |
| 28 | + IHttpContextAccessor context, |
| 29 | + BotSharpOptions options, |
| 30 | + EmailHandlerSettings emailPluginSettings) |
| 31 | + { |
| 32 | + _services = services; |
| 33 | + _logger = logger; |
| 34 | + _httpClientFactory = httpClientFactory; |
| 35 | + _context = context; |
| 36 | + _options = options; |
| 37 | + _emailSettings = emailPluginSettings; |
| 38 | + } |
| 39 | + public async Task<bool> Execute(RoleDialogModel message) |
| 40 | + { |
| 41 | + var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions); |
| 42 | + var recipient = args?.ToAddress; |
| 43 | + var body = args?.Content; |
| 44 | + var subject = args?.Subject; |
44 | 45 |
|
45 | | - try |
46 | | - { |
47 | | - var mailMessage = new MimeMessage(); |
48 | | - mailMessage.From.Add(new MailboxAddress(_emailSettings.Name, _emailSettings.EmailAddress)); |
49 | | - mailMessage.To.Add(new MailboxAddress("", recipient)); |
50 | | - mailMessage.Subject = subject; |
51 | | - mailMessage.Body = new TextPart("plain") |
52 | | - { |
53 | | - Text = body |
54 | | - }; |
55 | | - await HandleSendEmailBySMTP(mailMessage); |
56 | | - message.Content = $"Email successfully send over to {recipient}. Email Subject: {subject}"; |
57 | | - return true; |
58 | | - } |
59 | | - catch (Exception ex) |
| 46 | + try |
| 47 | + { |
| 48 | + var mailMessage = new MimeMessage(); |
| 49 | + mailMessage.From.Add(new MailboxAddress(_emailSettings.Name, _emailSettings.EmailAddress)); |
| 50 | + mailMessage.To.Add(new MailboxAddress("", recipient)); |
| 51 | + mailMessage.Subject = subject; |
| 52 | + mailMessage.Body = new TextPart("plain") |
60 | 53 | { |
61 | | - var msg = $"Failed to send the email. {ex.Message}"; |
62 | | - _logger.LogWarning($"{msg}\n(Error: {ex.Message})"); |
63 | | - message.Content = msg; |
64 | | - return false; |
65 | | - } |
| 54 | + Text = body |
| 55 | + }; |
| 56 | + var response = await HandleSendEmailBySMTP(mailMessage); |
| 57 | + _logger.LogWarning($"Email successfully send over to {recipient}. Email Subject: {subject} [{response}]"); |
| 58 | + message.Content = response; |
| 59 | + return true; |
66 | 60 | } |
67 | | - public async Task HandleSendEmailBySMTP(MimeMessage mailMessage) |
| 61 | + catch (Exception ex) |
68 | 62 | { |
69 | | - using (var smtpClient = new SmtpClient()) |
70 | | - { |
71 | | - await smtpClient.ConnectAsync(_emailSettings.SMTPServer, _emailSettings.SMTPPort, SecureSocketOptions.StartTls); |
72 | | - await smtpClient.AuthenticateAsync(_emailSettings.EmailAddress, _emailSettings.Password); |
73 | | - await smtpClient.SendAsync(mailMessage); |
74 | | - smtpClient.Disconnect(true); |
75 | | - } |
| 63 | + var msg = $"Failed to send the email. {ex.Message}"; |
| 64 | + _logger.LogError($"{msg}\n(Error: {ex.Message})"); |
| 65 | + message.Content = msg; |
| 66 | + return false; |
76 | 67 | } |
77 | 68 | } |
| 69 | + |
| 70 | + public async Task<string> HandleSendEmailBySMTP(MimeMessage mailMessage) |
| 71 | + { |
| 72 | + using var smtpClient = new SmtpClient(); |
| 73 | + await smtpClient.ConnectAsync(_emailSettings.SMTPServer, _emailSettings.SMTPPort, SecureSocketOptions.StartTls); |
| 74 | + await smtpClient.AuthenticateAsync(_emailSettings.EmailAddress, _emailSettings.Password); |
| 75 | + var response = await smtpClient.SendAsync(mailMessage); |
| 76 | + return response; |
| 77 | + } |
78 | 78 | } |
0 commit comments