Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new email locks outlook, forces email window as topMost

I am using the following code to read an email address from a DataGridView then create an Outlook email. This works perfectly, except that the new email is set as topMost and/or opened as a dialog window, meaning I cannot click or do anything else in Outlook while the new email window is open. This is problematic if I've got my new email open and I was trying to search or look up something back in my inbox. Also my application will not respond (is locked) until I close or send the email.

Is there a way to create a new email and still allow regular functionality? If I click the new email button from Outlook itself, I can have as many of these open as I want, use searching, etc.

The this.TopMost = false line is to hide my WinForms app and display the new email window in front.

try
{

      string emailString = resultsGrid[resultsGrid.Columns["Email"].Index, resultsGrid.SelectedCells[resultsGrid.Columns["Email"].Index].RowIndex].Value.ToString();

    if(emailString.Contains("mailto:"))
    {
        emailString = emailString.Replace("mailto:", "");
    }

    this.TopMost = false;

    // Create the Outlook application by using inline initialization.
    Outlook.Application oApp = new Outlook.Application();

    //Create the new message by using the simplest approach.
    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    oMsg.Subject = "";
    oMsg.To = emailString;
    oMsg.Body = "";
    oMsg.Display(true);


    oMsg = null;
    oApp = null;
}
catch (Exception ex)
{
    MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
}

What's also weird is if I write some stuff in the email and close it, I can save it. If I do this, when I open the email back up, it returns to it's locked state. I'm beginning to think this has something to do with how the email was created, so some setting or attribute is getting applied and saved with it.

like image 694
ikathegreat Avatar asked May 25 '12 18:05

ikathegreat


1 Answers

Try replacing this line:

oMsg.Display(true);

…with:

oMsg.Display(false);

Per the MailItem.Display documentation, the parameter’s name is Modal, and should be specified as:

True to make the window modal. The default value is False.

like image 74
Douglas Avatar answered Oct 05 '22 03:10

Douglas