Create a list of contacts from already received emails

We have multiple accounts on Office 365 and need a list of emails of the senders from emails received, within each account.

They all primarily use the web app, however we can set the accounts up on outlook if necessary.

I've tried searching but have turned up no results, unless I'm not using the right terms.
LVL 1
adamformanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark GalvinManaging Director / Principal ConsultantCommented:
Hi

So you want to list the SMTP addresses of all external senders of the emails in a particular Office 365 mailbox? And then apply that to all mailboxes?

I.E. My office 365 mailbox has 500 emails in the inbox from 73 different external senders. The end result you want is to list (in what format) the SMTP addresses of all 73 externals senders?

Thanks
Mark
Guy LidbetterCommented:
Hi There,

You can use Message Tracing for this. By default O365 keep 30 days so you can schedule a task to run every 30 days with this script:

Get-MessageTrace -StartDate ([DateTime]::Today.AddDays(-3)) -EndDate ([DateTime]::Today) | Where{-Not($_.Sender -like "*yourdomain.com")} | group -property sender | Select Name | Export-Csv "c:\temp\External_Senders_$((get-date ([DateTime]::Today.AddDays(-1)) -Format yyyyMMdd)).csv" -NoTypeInformation

Open in new window


This will create a CSV in the c:\temp directory with a name like "External_Senders_20150723.csv".
The CSV will just contain a condensed list of any senders that are not in the specified *yourdomain.com namespace.

Regards

Guy

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy LidbetterCommented:
By the way, if you would like help modifying this to use a CSV list of account you want reported, with a individual CSV for each one (or one csv for all of them), I'd be happy to help.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

adamformanAuthor Commented:
Hi all, thanks for the input.

The example mark described is exactly on point. I will try the command Guy provided and let you know how I get on from there.
Mark GalvinManaging Director / Principal ConsultantCommented:
Hi Guy

I have just tested this on my end. The CSV file gets created but the only thing in there is the field heading of 'Name'.

Any ideas why it didnt work?

PS - I changed the domain to my own :-)

Thanks
Mark
Guy LidbetterCommented:
@ Mark = I'm not sure, I'll see from my end if I can replicate.

@Adam = Updated to use a CSV with a Username and EmailAddress field. Creates a CSV per user...

#CSV Must contain Fields "EmailAddress" and "Username"
$Users = Import-CSV c:\Folder\users.csv 

Foreach ($User in $Users) {
	$Name = $User.Username
	Get-MessageTrace -StartDate ([DateTime]::Today.AddDays(-3)) -EndDate ([DateTime]::Today) -RecipientAddress $User.EmailAddress | Where{-Not($_.Sender -like "*yourdomain.com")} | group -property sender | Select Name | export-csv "c:\folder\$Name External Senders $((get-date ([DateTime]::Today.AddDays(-1)) -Format yyyyMMdd)).csv" -NoTypeInformation
	}

Open in new window

Guy LidbetterCommented:
Try add this to the beginning of the script:

$cred = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -Authentication Basic -ConnectionUri https://ps.outlook.com/powershell -AllowRedirection:$true -Credential $cred
Import-PSSession $session

Open in new window

Mark GalvinManaging Director / Principal ConsultantCommented:
@Guy - is that suggestion for me? Adding the $cred to the script?

Thanks
Mark
adamformanAuthor Commented:
Hey Guys,

I found the reason for the lack of entries. You need to change

group -property Sender

to

group -property SenderAddress

It seems to work fine with that change (and the change to the relevant domain)

Is there a way to add to the document who the sender was emailing to?
Guy LidbetterCommented:
Ah! Thanks Adam, good catch, the property "Sender" is for on Premise get-messagetrackinglogs cmdlet... I keep forgetting Microsoft have changed the argument names!!

You absolutely Can do that. However, what I have done is group the sender list to remove duplicates.
 Can you be more specific on your requirement?

Do you have a specific list of users you want to monitor? Or do you want to get ALL external senders to your domain and list the recipients as well?
Guy LidbetterCommented:
Oh, by the way, you may need to change the filter part of the script as well... from:
Where{-Not($_.Sender -like "*yourdomain.com")}

Open in new window

to
Where{-Not($_.SenderAddress -like "*yourdomain.com")}

Open in new window

adamformanAuthor Commented:
The initial solution provided didn't come back with results. I found the specific part that needed to be modified for the command to work, which I posted in my reply.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office 365

From novice to tech pro — start learning today.