c# vsto Outlook.ApplicationEvents_11_NewMailEx

murdochs

New Member
Outlook version
Outlook 365 32 bit
Email Account
IMAP
Using Visual Studio 2022
outlook 365 with IMAP account
VSTO addin. c#

Event : newmaileX to capture every incoming email. If certain subject, then I enter text into the body. If body is html, I find the tag and insert after. I have create a button on ribbon to test, and the email subject is altered and new text is in the body. fine so far. When the newmailex is fired, it runs my code, but sometimes it does not save the changes, and sometimes it does.

Why ?

if I add "mailItem.Save()" this works, but I then get duplicates of the same email. Sometimes upto 10 duplicates. Is this an IMAP thing ? Below is my code.

I have tried just VBA instead of the vsto, with newmailex and i get the same results.

Code:
    void NewMail_Event(String entryIDCollection)
    {
        Outlook.NameSpace outlookNS = this.Application.GetNamespace("MAPI");
        Outlook.MailItem mailItem = null;

        try
        {
            string sTag;
            string sBody;
            int nPos;
            sTag = "<table border=\"1\" cellspacing=\"0\"><tr bgcolor=\"#f5e942\"><td><font color=\"#FF0000\" size=\"4\">";
            sTag += "This email originated from outside and was not listed in our safe senders list.<br>";
            sTag += "DO NOT action, click on any links or open attachments unless you recognise the sender and know the content is safe.";
            sTag += "</font></td></tr></table><br>";
            string sSubject = null;
            string filter = "[EXTERNAL]";
            string[] sEmails = entryIDCollection.Split(',');
            foreach (string sEmail in sEmails)
            {
                mailItem = (Outlook.MailItem)outlookNS.GetItemFromID(sEmail, Type.Missing);
                
                if (mailItem.Subject != null)
                {
                    File.AppendAllText("c:\\1data\\desktop\\vstolog.txt", mailItem.Subject + "\r\n");
                    if (mailItem.Subject.Length >= 10)
                    {
                        if (mailItem.Subject.ToUpper().StartsWith(filter))
                        {
                            sSubject = mailItem.Subject.Substring(filter.Length, mailItem.Subject.Length - filter.Length);
                            mailItem.Subject = sSubject;

                            sBody = mailItem.HTMLBody;

                            if (sBody.Contains("<body>") || sBody.Contains("<BODY>"))
                            {
                                if (sBody.Contains("<BODY>"))
                                {
                                    sBody = sBody.Replace("<BODY>", "<body>" + sTag);
                                }
                                else
                                {
                                    sBody = sBody.Replace("<body>", "<body>" + sTag);
                                }
                            }
                            else
                            {
                                if (sBody.Contains("<body") || sBody.Contains("<BODY"))
                                {

                                    if (sBody.Contains("<body"))
                                    {
                                        nPos = sBody.IndexOf("<body", 0);
                                    }
                                    else
                                    {
                                        nPos = sBody.IndexOf("<BODY", 0);
                                    }
                                    nPos = sBody.IndexOf(">", nPos);
                                    sBody = sBody.Insert(nPos + 1, sTag);

                                }
                                else
                                {
                                    sBody = sTag + sBody;
                                }
                            }

                            if (mailItem.BodyFormat == OlBodyFormat.olFormatHTML)
                            {
                                mailItem.HTMLBody = sBody;
                            }
                            else
                            {
                                mailItem.Body = sBody;
                            }
                            //mailItem.Close(OlInspectorClose.olSave);
                        }
                    }
                }
                Marshal.ReleaseComObject(mailItem);
                mailItem = null;
            }
        }

        catch (Exception eX)
        {
            MessageBox.Show(eX.Message);
        }

        finally
        {
            if (mailItem != null) Marshal.ReleaseComObject(mailItem);
            mailItem = null;
            if (outlookNS != null) Marshal.ReleaseComObject(outlookNS);
            outlookNS = null;
        }
    }
 

murdochs

New Member
Outlook version
Outlook 365 32 bit
Email Account
IMAP
Using Visual Studio 2022
outlook 365 with IMAP account
VSTO addin. c#

Event : newmaileX to capture every incoming email. If certain subject, then I enter text into the body. If body is html, I find the tag and insert after. I have create a button on ribbon to test, and the email subject is altered and new text is in the body. fine so far. When the newmailex is fired, it runs my code, but sometimes it does not save the changes, and sometimes it does.

Why ?

if I add "mailItem.Save()" this works, but I then get duplicates of the same email. Sometimes upto 10 duplicates. Is this an IMAP thing ? Below is my code.

I have tried just VBA instead of the vsto, with newmailex and i get the same results.

Code:
    void NewMail_Event(String entryIDCollection)
    {
        Outlook.NameSpace outlookNS = this.Application.GetNamespace("MAPI");
        Outlook.MailItem mailItem = null;

        try
        {
            string sTag;
            string sBody;
            int nPos;
            sTag = "<table border=\"1\" cellspacing=\"0\"><tr bgcolor=\"#f5e942\"><td><font color=\"#FF0000\" size=\"4\">";
            sTag += "This email originated from outside and was not listed in our safe senders list.<br>";
            sTag += "DO NOT action, click on any links or open attachments unless you recognise the sender and know the content is safe.";
            sTag += "</font></td></tr></table><br>";
            string sSubject = null;
            string filter = "[EXTERNAL]";
            string[] sEmails = entryIDCollection.Split(',');
            foreach (string sEmail in sEmails)
            {
                mailItem = (Outlook.MailItem)outlookNS.GetItemFromID(sEmail, Type.Missing);
               
                if (mailItem.Subject != null)
                {
                    File.AppendAllText("c:\\1data\\desktop\\vstolog.txt", mailItem.Subject + "\r\n");
                    if (mailItem.Subject.Length >= 10)
                    {
                        if (mailItem.Subject.ToUpper().StartsWith(filter))
                        {
                            sSubject = mailItem.Subject.Substring(filter.Length, mailItem.Subject.Length - filter.Length);
                            mailItem.Subject = sSubject;

                            sBody = mailItem.HTMLBody;

                            if (sBody.Contains("<body>") || sBody.Contains("<BODY>"))
                            {
                                if (sBody.Contains("<BODY>"))
                                {
                                    sBody = sBody.Replace("<BODY>", "<body>" + sTag);
                                }
                                else
                                {
                                    sBody = sBody.Replace("<body>", "<body>" + sTag);
                                }
                            }
                            else
                            {
                                if (sBody.Contains("<body") || sBody.Contains("<BODY"))
                                {

                                    if (sBody.Contains("<body"))
                                    {
                                        nPos = sBody.IndexOf("<body", 0);
                                    }
                                    else
                                    {
                                        nPos = sBody.IndexOf("<BODY", 0);
                                    }
                                    nPos = sBody.IndexOf(">", nPos);
                                    sBody = sBody.Insert(nPos + 1, sTag);

                                }
                                else
                                {
                                    sBody = sTag + sBody;
                                }
                            }

                            if (mailItem.BodyFormat == OlBodyFormat.olFormatHTML)
                            {
                                mailItem.HTMLBody = sBody;
                            }
                            else
                            {
                                mailItem.Body = sBody;
                            }
                            //mailItem.Close(OlInspectorClose.olSave);
                        }
                    }
                }
                Marshal.ReleaseComObject(mailItem);
                mailItem = null;
            }
        }

        catch (Exception eX)
        {
            MessageBox.Show(eX.Message);
        }

        finally
        {
            if (mailItem != null) Marshal.ReleaseComObject(mailItem);
            mailItem = null;
            if (outlookNS != null) Marshal.ReleaseComObject(outlookNS);
            outlookNS = null;
        }
    }

Should I finish the code with "mailitem.save" ? Initally, it was inserting my tag in teh body without this line. but somewhere I have broken it, and now it does not work. the event fires each time, but the changes are not made in the email. If I add "mailitem.save" this is when I get the double up emails in the inbox. So I suppose, if I can get an answer if I should be using the save or not, then will give me somewhere to work from. thanks in advanced
 
Similar threads
Thread starter Title Forum Replies Date
A Not able to load an add-in for outlook 2007 developed in VSTO 2005 Outlook VBA and Custom Forms 2
T VSTO Outlook add-in: Categories not displaying correctly Outlook VBA and Custom Forms 3
O VSTO - Outlook Add-In - Get GAL data Outlook VBA and Custom Forms 1
K Outlook VSTO Add-in not visible in Trust Center after installing Outlook VBA and Custom Forms 1
M Will a VSTO C# Outlook 2007 Add-in work on Outlook 2010? Outlook VBA and Custom Forms 1
X Highlight block of text in an outlook mail using c#2008 vsto Outlook VBA and Custom Forms 1
W My VSTO 3.0 Outlook addin doesn’t load Outlook VBA and Custom Forms 1
N VSTO outlook 2007 addin installation problem Outlook VBA and Custom Forms 1
O Third-party VSTO 2005 SE plug-in for Outlook 2007: what should I signwith a publisher certificate? Outlook VBA and Custom Forms 3
O Outlook VSTO 2005 plug-in installer: CLSID = ? Outlook VBA and Custom Forms 3
E Opening WinForm in Outlook 2003 VSTO Addin with Wordmail enabled Outlook VBA and Custom Forms 1
M Develop for OL2003 on OL2007 machine using VS2008 and VSTO? Outlook VBA and Custom Forms 1
E VSTO 2008 how to bind Form Region controls to Item data fields Outlook VBA and Custom Forms 2
E VSTO 2008 how to bind Form Region controls to Item data fields Outlook VBA and Custom Forms 3
M VSTO deployment? Outlook VBA and Custom Forms 1
M VSTO C#: How do I declare an application scope variable? Outlook VBA and Custom Forms 2
S VSTO calling unmanged function Outlook VBA and Custom Forms 3
G Interop or VSTO or ? Outlook VBA and Custom Forms 2
M VSTO 2007 Addin auto-disabled on one machine but not another on installation? Outlook VBA and Custom Forms 1
O using VSTO to get Available Mailboxes Outlook VBA and Custom Forms 11
M Outlook2007 and VSTO, handle the Click on the Save Button in the IPM.Note dialog HOWTO? Outlook VBA and Custom Forms 4
M VSTO Outlook2007 Addin. After switching from 3.5 to 2.0 Framework i get "'MSB3185: EntryPoint not sp Outlook VBA and Custom Forms 1
C Problem installing VSTO AddIn on Vista Outlook VBA and Custom Forms 3
Rupert Dragwater Background colors not saving in Outlook 365 Using Outlook 11
petunia Outlook tasks module sunsetting? Exchange Server Administration 3
G Save emails as msg file from Outlook Web AddIn (Office JS) Outlook VBA and Custom Forms 1
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 3
U Outlook 2021 not showing contact cards in Searches Using Outlook 2
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 2
H Outlook 365 O365 outlook calendar item editing Using Outlook 1
J Outlook 365 html inline images Using Outlook 1
Rupert Dragwater How to get Outlook 365 to open from websites Using Outlook 5
S Why do I have to close and reopen Outlook for macros to work? Outlook VBA and Custom Forms 2
J Outlook 2021 ScanPST errors (yet again ... sorry): repair button missing Outlook 2021 Using Outlook 0
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
K Moved pst to new computer, now Gmail not coming into Outlook Using Outlook 7
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
F Running Scripts in Outlook 2021 Using Outlook 0
Nufc1980 Outlook "Please treat this as private label" auto added to some emails - Help. Using Outlook 3
S Outlook 2019 Custom outlook Add-in using Visual Studio Outlook VBA and Custom Forms 0
V Outlook macros no longer run until VB editor is opened Outlook VBA and Custom Forms 0
R Outlook 365 How to integrate a third-party app with Outlook to track email and sms? Using Outlook 2
e_a_g_l_e_p_i I can't believe what I am seeing when trying to install Outlook 2021 Using Outlook 9
Kika Melo Outlook Calendar deleted appointments not in Deleted Items folder Using Outlook 3
P How to get a QR code for automatic signin with Outlook for iOS Using Outlook 5
J Migrating Outlook Using Outlook 1
Retired Geek Outlook for the MAC with Yahoo accounts now very broken Using Outlook 9
S Outlook 2002- "Send" button has disappeared. Help please. Using Outlook 1
L How Stop Outlook Nag Messages Using Outlook 1
TomHuckstep Remove Send/Receive All Folders (IMAP/POP) button from Outlook 365 Ribbon Using Outlook 1

Similar threads

Top