VB.Net Tutorial/Windows/Outlook

Материал из VB Эксперт
Перейти к: навигация, поиск

Outlook Email watcher

Imports System

public class MainClass
   Private Shared WithEvents objOMW As OutlookMessageWatcher
   public Shared Sub Main
        objOMW = New OutlookMessageWatcher()
   End Sub
    Private Shared Sub objOMW_EmailArrived(ByVal From As String, _
            ByVal Subject As String, _
            ByVal Message As Outlook.MailItem) _
            Handles objOMW.EmailArrived
        Console.WriteLine(Subject)
    End Sub
End class
Public Class OutlookMessageWatcher
    Private WithEvents objInboxItems As Outlook.Items
    Private objOutlook As Outlook.Application
    Public Event EmailArrived(ByVal From As String, _
        ByVal Subject As String, _
        ByVal Message As Outlook.MailItem)
    Public Sub New()
        objOutlook = New Outlook.Application()
        objOutlook.Session.Logon(NewSession:=False)
        objInboxItems = objOutlook.Session.GetDefaultFolder (Outlook.OlDefaultFolders.olFolderInbox).Items
    End Sub
    Private Sub objInboxItems_ItemAdd(ByVal Item As Object)Handles objInboxItems.ItemAdd
        Dim objNewMail As Outlook.MailItem
        objNewMail = CType(Item, Outlook.MailItem)
        RaiseEvent EmailArrived(objNewMail.SenderName, objNewMail.Subject, objNewMail)
    End Sub
End Class