Forum Discussion
How to count messages sent from a specific mailbox
- May 13, 2024Are you looking for messages sent during a specific time period or? The report in the admin center will give you this data for preset periods (30-180 days), and yes, it's generated with some delay, usually 2-3 days.
If you want more up-to-date data, use the message trace:
Get-MessageTrace -SenderAddress email address removed for privacy reasons
The downside is that you can only cover limited time period with it, up to 10 days:
Get-MessageTrace -SenderAddress email address removed for privacy reasons -StartDate (Get-Date).AddDays(-10) -EndDate (Get-Date)
Another option would be to use the Graph API or EWS to fetch the content of the Sent Items folder for the mailbox. However, it's not mandatory for sent messages to be actually saved therein, so the results might not be what you expect.
If you want more up-to-date data, use the message trace:
Get-MessageTrace -SenderAddress email address removed for privacy reasons
The downside is that you can only cover limited time period with it, up to 10 days:
Get-MessageTrace -SenderAddress email address removed for privacy reasons -StartDate (Get-Date).AddDays(-10) -EndDate (Get-Date)
Another option would be to use the Graph API or EWS to fetch the content of the Sent Items folder for the mailbox. However, it's not mandatory for sent messages to be actually saved therein, so the results might not be what you expect.
Thanks for your feedback VasilMichev, thank you very much...
Well, I understand the reporting issues within the Admin Center and they are always very data rich. We periodically collect data based on the reports.
In my case, unfortunately they can be considered insufficient.
Look: I have a box - now two - that send alert messages to different recipients, which are the focal points, local leaders to move masses, remove population from regions to try to save lives.
In a situation that we call a public calamity - disaster - this is still, largely because of the recipients, the "best form of communication".
This box has exceeded the message sending limit within 24 hours. It exceeded 10K.
My idea is that I can generate a kind of "online" contactor. Be it Graph or "pure" Poweshell, but I run it and get the response: "100, 200, 5k, 6k messages sent" and with that I can make the necessary adjustments or guidelines, in order to avoid new blocking, blocking of the domain or even blocking the entire tenant.
Right now, it's one more crisis that we don't need =P
Returning to the solution:
- Using "Get-MessageTrace", in the current situation it's not even a problem for 10 days as I need the last 24 hours.
- The problem is that it brings me a limit of 1K. The alternative is to "break" into several searches. I would have to make a change at the time, but I'm not sure what time I could monitor.
I did some research, and putting it together "here and there" I came up with something like this, below, but you know when you're not sure that this would be correct. So I suggest you see if this would be the closest to what I need:
#Config Mail
$MAIL = '[Aqui meu Email]'
# Set the maximum number of items per query
$maxItemsPorConsulta = 1000
# Set the time range for the query (last 24 hours)
$startDate = (Get-Date).AddHours(-24)
$endDate = Get-Date
# Initialize a variable to store the total number of items retrieved
$totalItensRecuperados = 0
# Loop as long as there are more items to retrieve
while ($true) {
# Execute the query based on the current time range
$resultados = Get-MessageTrace -SenderAddress $MAIL -StartDate $startDate -EndDate $endDate -PageSize $maxItemsPorConsulta
# Check for recovered items
if ($resultados.Count -eq 0) {
Write-Host "Nenhum item encontrado."
break
}
# Process the results here (e.g. save to a file, process each item, etc.)
# Update the total number of items recovered
$totalItensRecuperados += $resultados.Count
# Check if you have reached the maximum item limit
if ($resultados.Count -lt $maxItemsPorConsulta) {
Write-Host "Todos os itens foram recuperados."
break
}
# Update start date for next appointment
$startDate = $resultados[-1].Received -as [datetime]
# If you wish, you can add a delay here to avoid overloading the server
Start-Sleep -Seconds 1
}
# Display the total items recovered
Write-Host "Total de itens recuperados: $totalItensRecuperados"
The issue is that: Whenever the routine runs it will search from 24 hours back to the time the execution starts, which would mean I wouldn't have the exact query from the last 24 hours of a mailbox. At least that's what I understood.
There is still the issue of Graph. If I already get lost using Powershell, imagine Graph.
Still, I thank everyone for their support, especially you Vasil Michev.
I believe I may be getting somewhere
Big hug to everyone
Best regards