c# outlook Sendeventhandler opening object only once

Asked

Viewed 197 times

4

In my application I use Outlook Sendeventhandler to capture the click on the "Send" button of the email in outlook and open a screen to register observation regarding the email that was sent.

I have the main class where I mail:

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 Event is executed in the first email sent. Ex: I have two different emails, in one object "emailEnviado" the Listpendenciaid has 11 items, in the second email the list has 4 items.

When sending the email is the first or the second, the object comes with the correct properties. If I send the first email with the list of 11 objects first it comes correct and if I send the second email first it comes the list of 4 correct objects. But the event "Emailenviadoevent" is only triggered the first time I send an email and the others that are open and send after do not trigger the event.

  • I tried to reproduce the problem on my machine but failed to understand better what functionality you are wanting to develop. Could detail the context of the application and the specific functionality?

  • Inside the event "Emailenviadoevent" I call the screen "Historicoemailcadastro" that serves to register an observation. Context: 1 - Pendance query screen. This screen lists all backlogs analysts should charge customers for.

  • 2 - Sending email: Analysts select the backlog they want to charge and click on a button to automatically generate emails. These emails are generated by the company group, so the analyst can select 20 backlogs and generate 3 emails by different groups (Group 1 - 10 backlogs, Group 2 - 3 backlogs, Group 4 - 7 backlogs).

  • 3 - The analyst registers the observation using the "Historicoemailregister" screen to each email sent.

  • Today I only have an independent observation of how many emails are generated, because I create only one object "Emailenviado" where I have all the pending ids that it generated"

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.