I forgot to add that I would not write MTA which listens to SMTP port. I don't trust myself to do it in proper, secure, DOS resistant way. Instead I would plug script written with usage of pperl into some "real" MTA like postfix or sendmail. Such script still is subject for security compromises but their changes are lower since your script would be simplier (thus lower probability of security holes) and it would handle only portion of possible attacks.
--
Ilya Martynov
(http://martynov.org/)