none
call additional method in send/receive method

    Question

  • Hi All,

    I am creating an outlook addin (for OL 2003-2010) that receives a list of mail items from a web service (as a DataTable) and creates the mail items in a custom folder in OL for display. To achieve this I am using MAPI and Redemption objects.

    My question is, I need to add my "GetNewMailsFromWebService" method to fire whenever the send/receive event fires on OL. That is, it should be called whether the automatic OL send/receive method fires or the user does a manual update by selecting the send/receive command/button. Also, I obviously do not want the default send/receive behavior to stop for any other accounts that the user may have on OL.

    I guess it should be something like binding an event handler that calls my method, but I have no idea what event to bind to. All forums/tutorials I came across address manually triggering send/receive only.

    I am using VS2010 and C#.

    Thanks!

    -Thimila

    Wednesday, December 05, 2012 11:34 PM

Answers

  • Application.Session.SyncObjects[1].SyncStart += your method;

    of course you have to keep SyncObject on which you attached your event in some global variable so it does not get garbage collected and events stop comming in.

    Wednesday, December 12, 2012 8:26 AM
  • What is your code that calls SaveAs?

    As for the Sender, set the SentOnBehalfOf property as well

    mailItem.Sender = sender;
    mailItem.SentOnBehalfOf = sender;
    mailItem.Save();


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Wednesday, December 12, 2012 1:06 PM

All replies

  • Session has SyncObjects collection, first item there usually is SyncAll object. Attach to SyncStart event on it.
    Thursday, December 06, 2012 7:41 AM
  • So do you want Outlook to treat your newly created items as new messages and display new mail notifcation and run rules on them?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, December 06, 2012 2:03 PM
  • Hi DamianD,

    Thanks for the reply, I will try that.

    Yes Dmitry.

    Newly created messages should be treated as mails, they will be set as read/unread based on a field value (say bool isRead) using RDOMail.UnRead (am I correctly doing that?). If it is an unread mail, I want outlook to show the new mail received notification.

    But eventually I want to extend the add in so that when the user replies to my mail items, it should bring up custom new/reply window that will send the response to the webService, not send through the outlook profile.

    However, I am unsure what you mean by "run rules on them".

    Thursday, December 06, 2012 10:04 PM
  • If you call RDOMail.SaveAs with an empty path, Redemption will call IMessage::SaveChanges instead of exporting the message, and the second argument will be assumed to be flags to be passed to SaveChanges. You will probably need ITEMPROC_FORCE | NON_EMS_XP_SAVE (http://blogs.msdn.com/b/stephen_griffin/archive/2008/01/11/outlook-rules-at-least-when-you-tell-it-to.aspx)

      ITEMPROC_FORCE = &H00000800

      NON_EMS_XP_SAVE = &H00001000

      KEEP_OPEN_READWRITE = &H00000002

      set Session = CreateObject("Redemption.RDOSession")

      Session.MAPIOBJECT = Application.Session.MAPIOBJECT

      set NewMsg = Session.GetDefaultFolder(olFolderInbox).Items.Add

      ....

      NewMsg.SaveAs "", KEEP_OPEN_READWRITE or ITEMPROC_FORCE or NON_EMS_XP_SAVE

      set NewMsg = Nothing


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, December 07, 2012 12:05 AM
  • Hi Dmitry,


    I have been  trying to save the new mail items using the SaveAs method as suggested, however, I keep getting the following error message:

    "SaveAs() - only olMsg, olTemplate, olTNEF, olHTML, olVCard, olTxt(0) and olRRFC22 are currently supported."


    Also, I wasn't able to add my 'refresh' method to the send/receive objects collection according to DamianD's suggestion. Could you give me a code example on how to do it?


    One more question, I have been trying to resolve this to no avail for a while now:

    When adding the sender to a newly created mail item, the sender name is not displayed on the mail object on the outlook explorer panel. Even though the sender is shown in the mail item, it is not shown on the mail items list. 

    following is my code for adding the sender:

    Redemption.RDOMail mailItem = rdoFolder.Items.Add();

    ...

    string strEntryID = mailItem.Session.AddressBook.CreateOneOffEntryID("Greg xxx", "SMTP", "greg@dummy.com", false, true);

    Redemption.RDOAddressEntry sender = mailItem.Session.AddressBook.GetAddressEntryFromID(strEntryID);

    mailItem.Sender = sender;

    Could you point out to me why the sender name is not displayed? 



    Wednesday, December 12, 2012 8:22 AM
  • Application.Session.SyncObjects[1].SyncStart += your method;

    of course you have to keep SyncObject on which you attached your event in some global variable so it does not get garbage collected and events stop comming in.

    Wednesday, December 12, 2012 8:26 AM
  • What is your code that calls SaveAs?

    As for the Sender, set the SentOnBehalfOf property as well

    mailItem.Sender = sender;
    mailItem.SentOnBehalfOf = sender;
    mailItem.Save();


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Wednesday, December 12, 2012 1:06 PM
  • Thanks Damian, I finally got it to work. :)

    Dmitry,

    Thanks for the SentOnBehalfOf property solution. That solved my problem!

    The saveAs method, I am calling as follows:

    UInt32 ITEMPROC_FORCE = 0x00000800;
    
    Redemption.RDOFolder rdoFolder = rdoSession.GetFolderFromID(myCustomFolderID);
    
    Redemption.RDOMail mailItem = rdoFolder.Items.Add();
    
    ...
    
    mailItem.SaveAs("", ITEMPROC_FORCE);

    Thursday, December 13, 2012 1:02 AM
  • Hi Thimila,

    Will you mark Dmtry's reply? Please feel free to let us know if you have concern.


    Forrest Guo | MSDN Community Support | Feedback to manager

    Friday, December 14, 2012 2:23 PM
    Moderator