none
Visual Basic - Event when a specific email is received on Outlook RRS feed

  • Question

  • Hi all,

    i'm started some weeks ago writing Visual Basic with Visual Studio 2019.

    My application send an email with a specific subject like "Technical change XYZ" to a list of approver, who receive this mail can approve or refuse replying with "approve" or "deny" response.

    How can I continuously read the emails I receive so as to trigger an event as soon as the right email is received?


    Thursday, April 18, 2019 6:28 AM

All replies

  • Hi,

    use Application.NewMailEx event (Outlook)

    monitor outlook:

      Public Sub _tMonitorOutLook()
            Dim outLookApp As Application = New Application()
            AddHandler outLookApp.NewMailEx, AddressOf outLookApp_NewMailEx
            MessageBox.Show("monitor Outlook")
    
            While True
                Thread.Sleep(10)
            End While
        End Sub
    Private Shared Sub outLookApp_NewMailEx(ByVal EntryIDCollection As String)
            Dim outLookApp As Application = New Application()
            Dim outLookNS As [NameSpace] = outLookApp.GetNamespace("MAPI")
            Dim outLookFolder As MAPIFolder = outLookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
            Dim storeID As String = outLookFolder.StoreID
            Dim mail As MailItem = CType(outLookNS.GetItemFromID(EntryIDCollection, storeID), MailItem)
            Dim rx As Regex = New Regex("xxxx")
            If rx.IsMatch(mail.Subject) Then
    
            End If
        End Sub

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 18, 2019 7:31 AM
  • Thank you Alex,

    could you explain me which modules or extensions I have to import?

    Thank you so much

    Thursday, April 18, 2019 9:53 AM
  • Hi,

    reference dll

    Imports Microsoft.Office.Interop.Outlook

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 18, 2019 9:57 AM
  • Xed,

    You say Outlook, do you mean by using the Exchange Server, Microsoft Office or Outlook inside Windows 7/10?


    Success
    Cor

    Thursday, April 18, 2019 12:43 PM
  • @Cor,

    I mean from Outlook on Windows 10.

    My application is designed to produce a string, this string is send by local outlook to a specific person, this person will validate this string. If string is valid he reply to mail and send it back to me. Application have to be in loop in order to individuate this string and give the "green light" to perform other operations.

    @Alex,

    Thank you for your code, it's very useful! But now I have 2 questions:

    - When I trigger the event with this part of code I have to stop the loop because application is not respond anymore, it's as if the loop never ends.

    If rx.IsMatch(mail.Subject) Then
                MsgBox("RECEIVED!")
            End If

    - How I can pass a string into the loop from another sub? I have to pass this where hashMail is a causal string generated.

    "GOVERNANCE-APPROVAL for HASH " & hashMail

    Thank you so much for support!


    Friday, April 19, 2019 8:12 AM
  • @Cor,

    I mean from Outlook on Windows 10.

    My application is designed to produce a string, this string is send by local outlook to a specific person, this person will validate this string. If string is valid he reply to mail and send it back to me. Application have to be in loop in order to individuate this string and give the "green light" to perform other operations.

    @Alex

    Ok I found all module and dll to be imported, but I have a doubt. It this code able to be in loop? 

    Hi,

    Yes, I test it ,it works well.

    test full code:

    Imports System.Text.RegularExpressions
    Imports System.Threading
    Imports Microsoft.Office.Interop.Outlook
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            _tMonitorOutLook()
        End Sub
        Public Sub _tMonitorOutLook()
            Dim outLookApp As Application = New Application()
            AddHandler outLookApp.NewMailEx, AddressOf outLookApp_NewMailEx
            MessageBox.Show("start monitoring outlook")
    
            While True
                Thread.Sleep(10)
            End While
        End Sub
    
        Private Shared Sub outLookApp_NewMailEx(ByVal EntryIDCollection As String)
            Dim outLookApp As Application = New Application()
            Dim outLookNS As [NameSpace] = outLookApp.GetNamespace("MAPI")
            Dim outLookFolder As MAPIFolder = outLookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
            Dim storeID As String = outLookFolder.StoreID
            Dim mail As MailItem = CType(outLookNS.GetItemFromID(EntryIDCollection, storeID), MailItem)
            'Dim rx As Regex = New Regex("xxxx")
            'If rx.IsMatch(mail.Subject) Then
    
            'End If
            MsgBox("received e-mail" & mail.Subject)
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, April 19, 2019 8:44 AM
  • Hi Alex, have you had a chance to take a look at previously message?
    Wednesday, April 24, 2019 6:54 AM
  • @Alex,

    Thank you for your code, it's very useful! But now I have 2 questions:

    - When I trigger the event with this part of code I have to stop the loop because application is not respond anymore, it's as if the loop never ends.

    If rx.IsMatch(mail.Subject) Then
                MsgBox("RECEIVED!")
            End If

    - How I can pass a string into the loop from another sub? I have to pass this where hashMail is a causal string generated.

    "GOVERNANCE-APPROVAL for HASH " & hashMail

    Thank you so much for support!


    Hi,

    1. When I trigger the event with this part of code I have to stop the loop because application is not respond anymore, it's as if the loop never ends.

    add code:

      If rx.IsMatch(mail.Subject) Then
                RemoveHandler outLookApp.NewMailEx, AddressOf outLookApp_NewMailEx
                MsgBox("received e-mail" & mail.Subject)
            End If

    2.How I can pass a string into the loop from another sub? I have to pass this where hashMail is a causal string generated.

     Dim rx As Regex = New Regex(a function of generating a string)
            If rx.IsMatch(mail.Subject) Then
                RemoveHandler outLookApp.NewMailEx, AddressOf outLookApp_NewMailEx
                MsgBox("received e-mail" & mail.Subject)
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 25, 2019 2:07 AM
  • Hi alex,

    I have always same issue, I'm unable to pass value... from this private shaed sub I can't launch any external functions.

    Friday, May 3, 2019 1:58 PM
  • Hi alex,

    I have always same issue, I'm unable to pass value... from this private shaed sub I can't launch any external functions.

    Hi,

    Sub has no return value, it should not pass the value, you can use function to pass the value.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 7, 2019 1:49 AM