Link to home
Start Free TrialLog in
Avatar of Andy Brown
Andy BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Reading the email address stored within the From field

I am developing a function within my Access database that lets us handle bounce backs and "Out of office" emails.  To do this, I link directly into Outlook via ADO and then pull in the relevant data.  However, on some of the "Out of office" emails it has the persons name within the FROM field but no email address (although I can see it on the screen).  I am guessing that it is within the email properties, but i do not know how to access this data.

Currently (after the connection string), I am simply using a very simple test string to select the emails within a specific folder:
SELECT * FROM [Bounced emails]

But as mentioned above, this doesn't give me everything.  Any ideas?
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image


<I link directly into Outlook via ADO and then pull in the relevant data.>
...p"ull in the relevant data" from what?

What is this "[Bounced emails]" table/query?
What is it based on, what fields are in it..?
...etc
Avatar of Andy Brown

ASKER

Sorry about that.

I have create a folder (within the inbox) called "Bounce emails", which is where I drag all of my bounced emails to.  I look through this folder for emails and handle them accordingly.  As for the fields available for each email - it has everything I need (Subject, Body etc), but the FROM field only contains the persons name, not their email address (even though it is visible on the screen).

I have searchd through all of the available fields that are available - but there doesn't appear to be a "Sent From" type field, which I am guessing is in another table/location.

I'm not sure if this helps, if you need anything else please let me know.

Thanks for the help.
From what I remember this has to do with Outlook being able to reference the Recipients Name and their email address.
In a linked In-box table, the email field does not appear to be visible, but the name (the "From" field does).

But the email address is in fact in the email.
It can be seen if you right-click the email and select: Message Options
The innards of the email (and the senders email address) can be seen under the "Message Headers" section.

Perhaps there is  another connection technology that reveals this data...?


I had thought that you could also link in the Contacts table and create a query with both outlook tables.
Then link both tables on the "Normalized Subject" and "From" fields.
But this produced no results..  (Perhaps you can investigate this technique further...?)

Let's see what other experts post.

JeffCoachman
Great stuff Jeff,

It's a tricky one as the Contact will not exist within Outlook, so that ones out.

The annoying thing is that when you pull in the FROM field value, it only has the persons name (in most 'Out of office' responses).  However, there is definitely somewhere containing this info - I just need to get at it.

Thanks again for all of your help.
Again, perhaps there is another connection technology that can get at this data...

To be sure, click the "Request Attention" link and ask that the Exchange zone be added to this Q.

Until then, let's see if another Expert chimes in...
Will do, but I'm hoping it's simply a table that we need to find.
The field you want is SenderEmailAddress.  The problem is, assuming that I'm remembering correctly, that ADO doesn't return that field.  I believe it returns a fixed set of fields just as an Access table linked to Outlook does.  You can access Outlook directly via it's object model and get all the fields.  Are you interested in that approach?
Thanks BlueDevilFan

I think think your right about ADO only returning a few of the fields.  As for using the object model, that might do the trick, wish i'd have thought of that.

I'm trying to get the SenderEmailAddress, Subject and Body, of records within a folder called "Bounced emails".  Ifyou have anything, or could point me in the right direction - it would be greatly appreciated.

Thanks again.
Where is the Bounced Emails folder?  Is it under the Inbox, at the same level as the Inbox, or somewhere else?
Sorry about that,

Inbox\Dev\Bounced Emails
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There's always something :)

I got a compile error, variable not defined on "olFolderInbox" within the following string.

Set olkFld = olkSes.GetDefaultFolder(olFolderInbox).Folders("Dev").Folders("Bounced Emails")

I don't currently have a reference to the Outlook object (but it looks like you've used late binding)

Shouldn't you really have accepted the post by BlueDevilFan as the "Solution"...?

https://www.experts-exchange.com/questions/26902792/Reading-the-email-address-stored-within-the-From-field.html?cid=238#35200298
...is not really a "Solution", it is just you agreeing to give the suggestion a try...
Sure, but I'm pretty confident this will do the job. I just need to sort out that variable (everything else makes sense).

Thanks though.
Yes, I used late binding.  You can change that if you want to.  Change olFolderInbox to 6, or define a constant with that name and set its value to 6.  Sorry, I write the code in Outlook using early binding and then typically change to late binding when I post.  I forgot to fix that constant.

What Jeff is saying about the way you're closing is that you're accepting your own comment as the answer and not awarding any points.  In essence it's like saying that you solved the issue yourself.  How about accepting my post as the answer and awarding points to it?
AndrewDBrown,

Yes, again, if you look at yopur post (that you acceped as the solution), you see that it is not really a solution, again it is only your agreement to try the suggestion.

I think you may have just selected your own post by accident.

Please click the "Request Attention" link for help with accepting BlueDevilFan's post, which *is* actually the "Solution"

;-)

JeffCoachman
Dohhhhhhh......sorry about that.

I've now replaced the olFolderInbox with 6, which seems to work fine.

However, I now get "Invalid procedure or argument" on the following line:
Set olkSes = olkApp.GetNamespace("Outlook")

PS - I'll request attention now, once again really sorry about that.



Mistake - sorry about that.  Please assign the points to BlueDevilFan

Thank you.
No problem.  These things happen.

What version of Outlook are you using?
2007 (although) it could end up on 2003/2010.

Thanks
For 2007 and 2010 you can change this line

Set olkSes = olkApp.GetNamespace("Outlook")

to

Set olkSes = olkApp.GetNamespace(olkApp.DefaultProfileName)

For 2003 you must either know the profile name or read it from the registry.  I think I have a code fragment here somewhere that does that.  Let me know if you need it and I'll find and post it.
Same error I'm affraid.  If I run "debug.print olkApp.DefaultProfileName", it returns "Outlook" so that bit is ok.
If it helps I know the full string for that folder:

outlook:\\Personal Folders\Inbox\Dev\Bounced Emails
Is your mail on an Exchange server?  Is the Inbox in that path the one where all your mail is delivered?
Most of the time it will be Exchange, but it could be Outlook.

And yes.

I bet you wished you never looke at this question.....
Outlook has a set of default folders (inbox, calendar, etc.).  Those folders are in the message store (mailbox or PST file) where mail is delivered.  It's unusual for an Exchange server account to deliver mail to a PST file.  Mail is typically delivered to the mailbox which is on the Exchange server.  That would make the Inbox in the mailbox the default, not the one in a PST file.  The command is getting the default inbox.  If that's the inbox in the Exchange mailbox and not the inbox folder in the PST file, then the command wouldn't find the folder you want.  So, are you sure that the inbox folder in the PST file is the default?

No, I don't wish that.  I'm here to help.
Thanks BlueDevilFan - I'll take another look in the morning (when I'm a little more awake).

Really appreciate your help today, I'll post back tomorrow.
It's amazing what a good night's sleep can do.  I changed one reference (Outlook to MAPI - see code below) and it pretty much worked, except now Outlook is coming up with an annoying security warning.

I think I'm going to post this as another question.

Thank you so much for the help - it really has been an interesting exercise.
Sub ReadBounced()

    Dim olkApp As Object, olkSes As Object, olkFld As Object, olkMsg As Object
    Set olkApp = CreateObject("Outlook.Application")
    
    'Set olkSes = olkApp.GetNamespace("Outlook")
    Set olkSes = olkApp.GetNamespace("MAPI")
    
    olkSes.Logon
    Set olkFld = olkSes.GetDefaultFolder(6).Folders("Dev").Folders("Bounced emails")
    For Each olkMsg In olkFld.Items
        With olkMsg
            Debug.Print .SenderEmailAddress
        End With
    Next
    Set olkMsg = Nothing
    Set olkFld = Nothing
    olkSes.Logoff
    Set olkSes = Nothing
    Set olkApp = Nothing
    
End Sub

Open in new window

Outlook.JPG
My fault.  Where was my brain at?  

You should not get that warning on Outlook 2007 or 2010 unless security is misconfigured or Windows cannot detect an acceptable anti-virus/malware solution.  You will get that warning on Outlook 2003.  While it cannot be turned off on Outlook 2003 and earlier there are ways to work around it.  Here are some of those ways.

1.  Sign the code.  Here's a link to instructions on doing that: http://msdn.microsoft.com/en-us/library/aa155754(office.10).aspx
2.  Use ClickYes (http://www.contextmagic.com/express-clickyes/), a small utility that'll click the Yes button for you.  It creates a security hole though, since a virus could start sending messages and ClickYes would click the Yes button for it too.  
3.  Use Redemption (http://www.dimastr.com), a COM library that enables code to safely bypass Outlook security.


The question is still set to be closed by accepting your comment.  To prevent that you can cancel the close and then go through the closure process a second time this time selecting a comment as the solution and awarding points.  Neither Jeff nor I are admins of the system and we can't change the way a question is closed.  The admins don't watch the questions so they won't see your comment in post 35200991 requesting a change in the way the question is closed.  You should see a button on the page that cancels the close.  Clicking it should cancel the close and allow you to restart the process.  If not, then I can post an objection to the closure.
Please can you assign all points to BlueDevilFan.  Thank you.
Thanks BlueDevilFan (yet again),

I'm not sure if I have closed/flagged this questions off correctly, but can't see anywhere else to do it from.  I have objected (above)....
I hope this has worked....
You're welcome.  Glad I could help.