using System;
using System.Runtime.InteropServices;
using BulkArchiveMailer.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace BulkArchiveMailer.AddIn
{
/// 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.
public sealed class MailSendOutcome
{
public SendJob Job { get; set; }
public bool Success { get; set; }
public string ErrorMessage { get; set; }
public bool WasDraft { get; set; }
}
///
/// 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.
///
public sealed class MailSender
{
private readonly Outlook.Application application;
private readonly string signatureHtml;
/// Live Outlook.Application (from ThisAddIn.Application) used to create mail items.
///
/// 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.
///
public MailSender(Outlook.Application application, string signatureHtml)
{
this.application = application ?? throw new ArgumentNullException(nameof(application));
this.signatureHtml = signatureHtml ?? string.Empty;
}
/// Row to send. Caller is responsible for only passing selectable (Ready/AlreadySent), checked rows - this method does not re-validate Status.
/// 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.
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 += "
" + 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);
}
}
}
}
}