0

I am creating a program that sends a text message and then depending on the reply I want to perform a specific action. Anyways here is my code:

using Microsoft.Office.Interop.Outlook;
using Outlook = Microsoft.Office.Interop.Outlook;

static void Main()
{                        
    var outlook = new Microsoft.Office.Interop.Outlook.Application();

     // fire event when a new email arives
     outlook.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(oApp_NewMailEx);

     // etc
}

static void oApp_NewMailEx(string EntryIDCollection)
{
    var outlook = new Microsoft.Office.Interop.Outlook.Application();
    MailItem temp = (MailItem)outlook.Session.GetItemFromID(EntryIDCollection, Missing.Value);

    var body = temp.Body; // the body of the email is null! I tried waiting and it is null until I open it...
    Console.WriteLine(body);
}

This part is not important but I send the "text message" with this function:

// send text message "att.txt.net only works with at&t phones"
public static int SendEmail(string recipeint = "9546543930@att.txt.net")
{
        try
        {
            // 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);



            Outlook.Accounts accounts = oMsg.Session.Accounts;
            foreach (Outlook.Account account in accounts)
            {
                // When the e-mail address matches, send the mail.
                if ( account.SmtpAddress.Contains("gmail") )
                {
                        oMsg.SendUsingAccount = account;                            
                        break;
                }
            }

            // If you want to, display the message.
            oMsg.Display(true);  //modal


            //Add a recipient.                
            Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add(recipeint);
            oRecip.Resolve();

            //Set the basic properties.
            oMsg.Subject = "This is the subject of the test message";
            oMsg.Body = "This is the text in the message.";


            //Add an attachment.
            // TODO: change file path where appropriate
            //String sSource = "C:\\setupxlg.txt";
            //String sDisplayName = "MyFirstAttachment";
            //int iPosition = (int)oMsg.Body.Length + 1;
            //int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
            //Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName);



            //Send the message.
            oMsg.Save();
            oMsg.Send();

            //Explicitly release objects.
            oRecip = null;
            //oAttach = null;
            oMsg = null;
            oApp = null;
        }

            // Simple error handler.
        catch (System.Exception e)
        {
            Console.WriteLine("{0} Exception caught: ", e);
        }

        //Default return value.
        return 0;
}

So I am able to send an email with my gmail account and when I reply to the text message the function oApp_NewMailEx gets executed with the id of the new email. I am able to get the subject but the body does not get downloaded until I hover my mouse over the email or open the email. I already waitied 2 minutes on another thread and then tried to see the body and it was still null.


Edit note to make this work I have:

enter image description here

It looks gray out because I did not run outlook as an administrator. If You run outlook as an admin you will be able to update the security settings.

I also imported the Microsoft Outlook 14.0 Object library as a reference.

Tono Nam
  • 34,064
  • 78
  • 298
  • 470

1 Answers1

0

Once I received the email I called the send and receive method. wait a little and then I was able to read the body.

  outlook.Session.SendAndReceive(false);
Tono Nam
  • 34,064
  • 78
  • 298
  • 470