Home > Back-end >  how to read outlook email in a directory files in vb.net
how to read outlook email in a directory files in vb.net

Time:06-11

I am new in vb.net development and I must read informations (subject, body, ...) in Outlook email files that are in a disk directory (D:\mails\to-read\message1.msg, D:\mails\to-read\message2.msg, ...).

Is it possible ?

Can you please explain me ? With example ?

Thanks for your help.

CodePudding user response:

If the Outlook object model (Outlook automation) is a possible option you can use the NameSpace.OpenSharedItem method which opens a shared item from a specified path or URL. This method is used to open iCalendar appointment (.ics) files, vCard (.vcf) files, and Outlook message (.msg) files. So, in the code you will get a MailItem object where you could get all the required properties.

Public Sub TestOpenSharedItem()
    Dim oNamespace As Outlook.NameSpace
    Dim oSharedItem As Outlook.MailItem
    Dim oFolder As Outlook.Folder   

    ' Get a reference to a NameSpace object.
    Set oNamespace = Application.GetNamespace("MAPI")'Open the Signed Message (.msg) file containing the shared item.
     Set oSharedItem = oNamespace.OpenSharedItem("C:\Temp\RegularMessage.msg")

    MsgBox oSharedItem.Subject

    oSharedItem.Close (olDiscard)
    Set oSharedItem = Nothing

    Set oSharedItem = Nothing
    Set oFSO = Nothing
    Set oNamespace = Nothing
End Sub
  • Related