Files
ssh-access-tui/outlook-bulk-sender/BulkArchiveMailer.AddIn/MailSender.cs
T
2026-07-23 12:38:13 +03:00

100 lines
3.9 KiB
C#

using System;
using System.Runtime.InteropServices;
using BulkArchiveMailer.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace BulkArchiveMailer.AddIn
{
/// <summary>Per-job result of a send attempt, returned by MailSender so the caller can log + report it without any of this class's own exceptions ever escaping.</summary>
public sealed class MailSendOutcome
{
public SendJob Job { get; set; }
public bool Success { get; set; }
public string ErrorMessage { get; set; }
public bool WasDraft { get; set; }
}
/// <summary>
/// Creates and sends (or, in test mode, saves as a Draft) one Outlook
/// mail item per SendJob via Interop. This is the only file that
/// actually creates/sends a MailItem - everything else builds up to
/// calling SendOne.
///
/// SendOne never throws: every failure is caught and reported back as
/// a failed MailSendOutcome, so a caller looping over a batch can
/// always move on to the next job without a try/catch of its own.
/// </summary>
public sealed class MailSender
{
private readonly Outlook.Application application;
private readonly string signatureHtml;
/// <param name="application">Live Outlook.Application (from ThisAddIn.Application) used to create mail items.</param>
/// <param name="signatureHtml">
/// HTML of the operator's default signature, already read from
/// disk by SignatureReader (Outlook does not auto-insert a
/// signature on programmatically created items - it must be
/// appended to HTMLBody by hand). May be null/empty if no
/// signature is configured; the mail is then sent without one.
/// </param>
public MailSender(Outlook.Application application, string signatureHtml)
{
this.application = application ?? throw new ArgumentNullException(nameof(application));
this.signatureHtml = signatureHtml ?? string.Empty;
}
/// <param name="job">Row to send. Caller is responsible for only passing selectable (Ready/AlreadySent), checked rows - this method does not re-validate Status.</param>
/// <param name="testMode">When true, the mail item is Save()'d (left in Drafts) instead of Send()'d - the safety net for trying the tool out before trusting it with real recipients.</param>
public MailSendOutcome SendOne(SendJob job, bool testMode)
{
if (job == null)
{
throw new ArgumentNullException(nameof(job));
}
Outlook.MailItem mailItem = null;
try
{
mailItem = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.To = job.Email;
mailItem.Subject = job.Subject;
var body = job.BodyHtml ?? string.Empty;
if (!string.IsNullOrEmpty(signatureHtml))
{
body += "<br><br>" + signatureHtml;
}
mailItem.HTMLBody = body;
if (!string.IsNullOrEmpty(job.ArchivePath))
{
mailItem.Attachments.Add(job.ArchivePath, Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
}
if (testMode)
{
mailItem.Save();
}
else
{
mailItem.Send();
}
return new MailSendOutcome { Job = job, Success = true, WasDraft = testMode };
}
catch (Exception ex)
{
return new MailSendOutcome { Job = job, Success = false, ErrorMessage = ex.Message, WasDraft = testMode };
}
finally
{
if (mailItem != null)
{
Marshal.ReleaseComObject(mailItem);
}
}
}
}
}