Results 1 to 18 of 18

Thread: [RESOLVED] System.Net to get email headers

  1. #1

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Resolved [RESOLVED] System.Net to get email headers

    Has anyone ever parsed email header text from a .eml file stored on disk? Just wondering if this is possible without 3rd party solution

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: System.Net to get email headers

    There's nothing built into .NET to do that so you'd have to do what those third-party solutions do internally, i.e. read in the raw data of the file and parse out the headers manually.

  3. #3

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: System.Net to get email headers

    Thank you JMC.

  4. #4

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: System.Net to get email headers

    After googling my eyes out I was able to hodgepodge a solution together that seems to be working.
    Here is what I came up with out of several different CodeProject and MSDN sources and C# conversions. This solution requires Interop

    Code:
    Imports Microsoft.Office.Interop.Outlook
    
    Public Class Form_Email
        Dim msgfile As String = "\\***\**\**\***\test.msg"
        Private app As Application = New Application()
        Private item As MailItem = TryCast(app.Session.OpenSharedItem(msgfile), MailItem)
        Private message As String = item.HTMLBody
        Private Header As String = TryCast(item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F"), String)
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MsgBox(Header)
        End Sub
    End Class

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: System.Net to get email headers

    Hi,

    you can use the OLEDB with a Sql-Statement, no need to ref. Outlook

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: System.Net to get email headers

    Quote Originally Posted by ChrisE View Post
    Hi,

    you can use the OLEDB with a Sql-Statement, no need to ref. Outlook

    regards
    Chris
    I will need to look into that!

    The OL solution, while working, produces the pesky "A program is trying to connect....bla bla bla... do you want to allow....bla bla"

  7. #7

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: System.Net to get email headers

    Quote Originally Posted by ChrisE View Post
    Hi,

    you can use the OLEDB with a Sql-Statement, no need to ref. Outlook

    regards
    Chris
    Also, are you referring to connecting to my OL pst file via oledb? that wont work for me

  8. #8

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: System.Net to get email headers

    Failing back to my OP. I cant seem to release/quit the app or item

    Once a file is selected it does what i want, but selecting the same file I get COMException: "We can't open '\\test.msg'. It's possible the file is already open, or you don't have permission to open it."

    Code:
                   If Ext.ToUpper = ".MSG" Then
                        Dim app As Outlook.Application = New Outlook.Application()
                        Dim item As Outlook.MailItem = TryCast(app.Session.OpenSharedItem(msgfile), Outlook.MailItem)
                        Dim message As String = item.HTMLBody
                        HeaderStr = TryCast(item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F"), String)
    
                        item.Close(Outlook.OlInspectorClose.olDiscard)
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(item)
                        app = Nothing
                    End If
    This solution is looking less desirable at the moment

  9. #9

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: System.Net to get email headers

    Cant seem to get anything to release the msg. Got to look at something else for a while.
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim app As Outlook.Application = New Outlook.Application()
            Dim ns As Outlook._NameSpace = Nothing
            Dim mailMessage As Outlook._MailItem = Nothing
            Try
                ns = app.Session
                mailMessage = CType(ns.OpenSharedItem("\\server\vol1\test.msg"), Outlook._MailItem)
    
                HeaderStr = TryCast(mailMessage.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F"), String)
                mailMessage.Close(Outlook.OlInspectorClose.olDiscard)
    
            Finally
                MessageBox.Show(HeaderStr)
                app.Quit()
                If ns IsNot Nothing Then Marshal.ReleaseComObject(ns)
                If mailMessage IsNot Nothing Then Marshal.ReleaseComObject(mailMessage)
                If app IsNot Nothing Then Marshal.ReleaseComObject(app)
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.CallInAirStrikeAndNukeThisDamnThing
            End Try
    
        End Sub

  10. #10

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: System.Net to get email headers

    Thinking this may have a thing or 2 to do with this here situation
    https://support.microsoft.com/en-us/...e-on-signed--m

  11. #11

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: System.Net to get email headers

    Anywho...

    I can confirm that indeed "After OL is IDLE for a certain time" (no idea how much time) that the mail item does finally release.

    Complete code. (probably dont need the garbage collector)
    Code:
                If FBrowser.ShowDialog() = DialogResult.OK Then
                    strFileName = FBrowser.FileName
                    Dim msgfile As String = FBrowser.FileName
                    Dim Ext As String = IO.Path.GetExtension(FBrowser.FileName)
    
                    If Ext.ToUpper = ".MSG" Then
                        Dim app As Outlook.Application = New Outlook.Application()
                        Dim ns As Outlook._NameSpace = Nothing
                        Dim mailMessage As Outlook._MailItem = Nothing
                        Try
                            ns = app.Session
                            mailMessage = CType(ns.OpenSharedItem(strFileName), Outlook._MailItem)
    
                            HeaderStr = TryCast(mailMessage.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F"), String)
                            mailMessage.Close(Outlook.OlInspectorClose.olDiscard)
                        
                        Finally
    
                            If ns IsNot Nothing Then Marshal.ReleaseComObject(ns)
                            If mailMessage IsNot Nothing Then Marshal.ReleaseComObject(mailMessage)
                            If app IsNot Nothing Then Marshal.ReleaseComObject(app)
                            GC.Collect()
                        End Try
    
                    End If
    
                End If

  12. #12
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: [RESOLVED] System.Net to get email headers

    I think a problem might be, "In COM, the order in which you destroy things matters." combined with "Using COM from .NET stinks."

    You create objects in this order:
    1. app As Application
    2. ns as some weird type
    3. mailMessage as _MailItem

    Logically speaking, the message belongs to the Session, and the Session belongs to the application. Probably part of releasing them involves telling their "owner" that they are being destroyed, as COM memory management often relies on reference counting.

    But you destroy them in this order:
    1. ns
    2. mailMessage
    3. app

    That's not quite right. ns is released before the mailMessage, which means it might not be possible to fully release the mailMessage anymore. Also, since you call app.Quit() before releasing anything, if the "sub objects" need anything from app they won't be able to get it.

    So you should release them in the order mailMessage -> ns -> app.Quit() -> app. That's more likely to work.

    But when dealing with COM from .NET, it could be any number of other things. That GC dance at the bottom is definitely familiar. I'm not 100% clear on why it's needed, but it is.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  13. #13

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: [RESOLVED] System.Net to get email headers

    Hey Sitten, thanks for taking a look. I believe that youre looking at a the work in progress code. I was calling app.quit just to see if closing the entire ordeal would release the file. nothing did.
    I thought sure calling in the airstrike would do it, but not even that was working
    GC.CallInAirStrikeAndNukeThisDamnThing
    In the end it came down to what the documentation said (jmc should be proud!)
    You can delay the attempt to delete the file, although there is no direct way to determine when Outlook will release the file lock.
    So the production code looks like this:
    Code:
                If FBrowser.ShowDialog() = DialogResult.OK Then
                    strFileName = FBrowser.FileName
                    Dim msgfile As String = FBrowser.FileName
                    Dim Ext As String = IO.Path.GetExtension(FBrowser.FileName)
    
                    If Ext.ToUpper = ".MSG" Then
                        Dim app As Outlook.Application = New Outlook.Application()
                        Dim ns As Outlook._NameSpace = Nothing
                        Dim mailMessage As Outlook._MailItem = Nothing
                        Try
                            ns = app.Session
                            mailMessage = CType(ns.OpenSharedItem(strFileName), Outlook._MailItem)
    
                            HeaderStr = TryCast(mailMessage.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F"), String)
                            mailMessage.Close(Outlook.OlInspectorClose.olDiscard)
                        
                        Finally
    
                            If ns IsNot Nothing Then Marshal.ReleaseComObject(ns)
                            If mailMessage IsNot Nothing Then Marshal.ReleaseComObject(mailMessage)
                            If app IsNot Nothing Then Marshal.ReleaseComObject(app)
                            GC.Collect()
                        End Try
    
                    End If
    
                End If

  14. #14
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: [RESOLVED] System.Net to get email headers

    Hi,

    what do you mean with 'get email Headers' ?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  15. #15

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: [RESOLVED] System.Net to get email headers

    Header info contains every last thing about the email message. in outlook you can see them by viewing the options of the email. this varies depending on the version of OL

  16. #16
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: [RESOLVED] System.Net to get email headers

    give a sample what Information you are trying to read
    Last edited by ChrisE; Dec 18th, 2017 at 08:58 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  17. #17

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: [RESOLVED] System.Net to get email headers

    Hey Chris. The email header is riddled with sensitive infos and by policy cant paste this, not even if I replace the many hostnames/ip resolutions.

    here is a sample i found on google, however.

    http://www.baruch.cuny.edu/bctc/EmailHeaderExample.htm

  18. #18
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: [RESOLVED] System.Net to get email headers

    Hi
    sorry, don't know how to get that Information

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width