In my application I use SendEventHandler from Outlook to capture the click on the "Send" button of the email in outlook and open a screen for the observation of the email that was sent.
I have the main class where I generate the emails:
public partial class PendenciaConsulta : UserControl
{
EmailEnviado emailEnviado = null;
private void btnEnviarEmail_Click(object sender, RoutedEventArgs e)
{
foreach(string grupo in ListaGrupo)
{
emailEnviado = new EmailEnviado();
emailEnviado.ListaPendenciaId = listaPendencia.Where(p=>p.ClienteGrupo == grupo).Select(p=>p.PendenciaId).ToList();
emailEnviado.PendenciaConsulta = this;
//Preparo o body
// ....
MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(emailEnviado.EmailEnviadoEvent);
((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).Close += new Microsoft.Office.Interop.Outlook.ItemEvents_10_CloseEventHandler(emailEnviado.EmailCanceladoEvent);
mailItem.BodyFormat = OlBodyFormat.olFormatHTML;
mailItem.HTMLBody = body.ToString();
mailItem.Display();
}
}
}
And the secondary class:
/// <summary>
/// Classe criada para poder receber a lista de ids de pendencias para ser tratadas no event.
/// </summary>
public class EmailEnviado
{
public List<int> ListaPendenciaId { get; set; }
public PendenciaConsulta PendenciaConsulta { get; set; }
public int StatusId { get; set; }
/// <summary>
/// Construtor para receber os parametros necessários
/// </summary>
/// <param name="listaPendenciaId"></param>
/// <param name="pendenciaConsulta"></param>
/// <param name="isITM"></param>
/// <param name="isRecepcao"></param>
public EmailEnviado(List<int> listaPendenciaId, PendenciaConsulta pendenciaConsulta, MailItem mailItem)
{
this.ListaPendenciaId = listaPendenciaId;
this.PendenciaConsulta = pendenciaConsulta;
}
/// <summary>
/// Criado pois os booleans não estavam sendo enviador através do construtor
/// </summary>
public EmailEnviado()
{
}
/// <summary>
/// Captura o evento se o usuário fechou sem enviar
/// </summary>
/// <param name="Cancel"></param>
public void EmailCanceladoEvent(ref bool Cancel)
{
}
public void EmailEnviadoEvent(ref bool Cancel)
{
Console.WriteLine("Inicio email enviado event");
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
Console.WriteLine("Inicio Dispatcher");
System.Windows.Window historicoEmailCadastro = new System.Windows.Window
{
Title = "Cadastro de Histórico de Email",
Content = new HistoricoEmailCadastro(ListaPendenciaId, PendenciaConsulta),
Width = 270,
Height = 260,
ResizeMode = ResizeMode.NoResize,
Topmost = true,
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
Console.WriteLine("Criou a window");
historicoEmailCadastro.ShowDialog();
if (StatusId == 4)
{
PendenciaController pendenciaControllerEmail = new PendenciaController();
pendenciaControllerEmail.PendenciaStatusAtualizarPorPendenciaId(ListaPendenciaId, StatusId, optionalData: true);
PendenciaConsulta.PendenciaStatusAtualizarPorPendenciaId(ListaPendenciaId, StatusId, optionalData: true);
}
else
{
PendenciaController pendenciaControllerEmail = new PendenciaController();
pendenciaControllerEmail.PendenciaStatusAtualizarPorPendenciaId(ListaPendenciaId, StatusId);
PendenciaConsulta.PendenciaStatusAtualizarPorPendenciaId(ListaPendenciaId, StatusId);
}
PendenciaConsulta.PesquisarOffLine();
});
}
}
The problem is that only the event is run on the first email sent. Eg: I generate two different emails, in an "emailed" object, the ListIndexId gets 11 items, in the second email the list gets 4 items.
When sending the email be the first or second, the object comes with the correct properties. If I send the first email first with the list of 11 objects it comes correct and if I send the second email first it comes the list of 4 correct objects. But the "EmailEventEvent" event is only triggered the first time I send an e-mail, and the others that are open and send later do not fire the event.