If you are trying to do this programmatically, use a service like
Here's a blog post comparing them http://www.sitepoint.com/email-as-a-service-part-2-sendgrid-mailgun-and-postmark/
I use Postmark and really like it. Fast, reliable, easy to implement, low-cost.
Unless your entire business IS email, do not attempt to write your own email delivery system.