I usually do it all in awk:
awk '/mail/ && NR <= 500 {...}' access.log
If you want N matched lines: awk '/mail/ && i < 500 {i++; ...}' access.log