using eprintServer.Enums; using eprintServer.DTOs; using MailKit.Net.Smtp; using MailKit.Security; using MimeKit; namespace eprintServer.Services; public class EmailService { private readonly string _host = "ssl0.ovh.net"; private readonly int _port = 587; private readonly string _username = "test@camel-soft.com"; private readonly string _password = "sBXsr{C0=naNZ9(P~M=_"; public async Task SendReportEmailAsync(byte[] pdfFile, ReportDateEum reportType, string adminEmail) { var message = new MimeMessage(); message.From.Add(new MailboxAddress("Print Dashboard", _username)); message.To.Add(new MailboxAddress("Admin", adminEmail)); // Determine Subject based on Enum message.Subject = reportType switch { ReportDateEum.DAILY => "Daily print report", ReportDateEum.WEEKLY => "Weekly print report", ReportDateEum.MONTHLY => "Monthly print report", ReportDateEum.YEARLY => "Yearly print report", _ => "Print dashboard report" }; var bodyBuilder = new BodyBuilder { HtmlBody = $@"

{message.Subject}

Hello Admin,

Please find attached the requested statistics report for the Print Dashboard.


Best Regards,
Automated Reporting System

" }; // Attach the PDF string fileName = $"{message.Subject.Replace(" ", "_")}_{DateTime.Now:yyyyMMdd}.pdf"; bodyBuilder.Attachments.Add(fileName, pdfFile, new ContentType("application", "pdf")); message.Body = bodyBuilder.ToMessageBody(); using var client = new SmtpClient(); try { // OVH uses StartTLS on port 587 await client.ConnectAsync(_host, _port, SecureSocketOptions.StartTls); await client.AuthenticateAsync(_username, _password); await client.SendAsync(message); } catch (Exception ex) { // Log the error but don't crash the app Console.WriteLine($"Email Error: {ex.Message}"); } finally { await client.DisconnectAsync(true); } } }