Вопрос

I've developed an Outlook plugin using C# where for each new mail received, i get(and save) the sender/subject/the body of email & the attachments. Well, the last 2 gave me a headache. I can see the sender and the subject of the new mail but for the body&attachments it seems that is a problem. I've used NewMailEx for getting the new mails in Inbox. The function looks like this:

private void Application_NewMailEx(string EntryIDCollection)
    {

        string[] entryIdArray = EntryIDCollection.Split(',');
        foreach (string entryId in entryIdArray)
        {


            try
            {
                Outlook.MailItem item =   (Outlook.MailItem)Application.Session.GetItemFromID(EntryIDCollection, null);

                string subj = item.Subject; //works
                string to = item.To; //works
                string bec = item.BCC; //does not work but dont care
                string body = item.Body; //DOES NOT SAVE THE BODY OF THE NEW MAIL RECEIVED


                string final = "Sender: " + item.SenderEmailAddress + "\r\n" + "Subject: " + subj + "\r\n" + "BCC: " + bec + "\r\n" + "TO: " + to + "\r\n\n" + "Body: " + body + "\r\n\n";
                System.IO.File.AppendAllText(@"D:\tmp\atr.txt", final);
                 //the result of item.attachments.count is always 0 , even though I've 
                 //sent mails with a different number of attachments. So the if 
                 //statement is false                                 
                if (item.Attachments.Count > 0) 
                {
                    for (int i = 1; i <= item.Attachments.Count; i++)
                    {
                        item.Attachments[i].SaveAsFile(@"D:\tmp\" + item.Attachments[i].FileName);
                    }
                }





               Marshal.ReleaseComObject(item);

            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
Это было полезно?

Решение

Where does the Item variable come from? You need to initialize it using Application.Session.getItemfromID().

Другие советы

I'm writing in VBA, so I feel like posting my code here would be a faux pas, but I have come to a solution using similar Object Libraries from the Outlook application that I think transfer well to your C++ intentions.

First of all, switching to POP3 certainly fixes this problem, but you're stuck using POP3, which is only ideal from a programming standpoint.

The solution I have found follows this algorithm:

 //Generate Outlook MailItem Object, "item"
    //If item.DownloadState is not 1,
        //item.Display
        //item.Close(1)
        //Perform end of code operations
        //Call Function that is identical to Application_NewMailEx but is not Application_NewMailEx because Application_NewMailEx is a function that is thrown during the incoming mail event.
    //Else,
        //Perform Intended Code

You see how calling a function identical to Application_NewMailEx creates a kind of loop because if item.DownloadState is not 1, you'll be calling that function again? I get that this isn't the most ideal coding practice, but I have scoured the internet and Outlook Application and Outlook Object Library experts have no idea how to solve this problem any other way (in fact, no one even proposes THIS solution)

For my full VBA Solution Check out:

https://superuser.com/questions/894972/outlook-strange-item-attachments-error/990968#990968

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top