-
Dec 15th, 2017, 09:18 AM
#1
Thread Starter
Fanatic Member
[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
-
Dec 15th, 2017, 09:22 AM
#2
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.
-
Dec 15th, 2017, 09:24 AM
#3
Thread Starter
Fanatic Member
Re: System.Net to get email headers
-
Dec 15th, 2017, 10:55 AM
#4
Thread Starter
Fanatic Member
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
-
Dec 15th, 2017, 12:07 PM
#5
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.
-
Dec 15th, 2017, 12:13 PM
#6
Thread Starter
Fanatic Member
Re: System.Net to get email headers
Originally Posted by ChrisE
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"
-
Dec 15th, 2017, 12:15 PM
#7
Thread Starter
Fanatic Member
Re: System.Net to get email headers
Originally Posted by ChrisE
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
-
Dec 15th, 2017, 01:11 PM
#8
Thread Starter
Fanatic Member
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
-
Dec 15th, 2017, 02:45 PM
#9
Thread Starter
Fanatic Member
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
-
Dec 15th, 2017, 03:24 PM
#10
Thread Starter
Fanatic Member
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
-
Dec 15th, 2017, 04:01 PM
#11
Thread Starter
Fanatic Member
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
-
Dec 15th, 2017, 04:44 PM
#12
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:
- app As Application
- ns as some weird type
- 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:
- ns
- mailMessage
- 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.
-
Dec 18th, 2017, 08:07 AM
#13
Thread Starter
Fanatic Member
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
-
Dec 18th, 2017, 08:19 AM
#14
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.
-
Dec 18th, 2017, 08:28 AM
#15
Thread Starter
Fanatic Member
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
-
Dec 18th, 2017, 08:29 AM
#16
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.
-
Dec 18th, 2017, 11:59 AM
#17
Thread Starter
Fanatic Member
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
-
Dec 19th, 2017, 05:50 AM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|