If you ever needed to write program that sends email, most likely you’ll need a SMTP server. Here’s how you can configure one on a Windows box using hMailServer.
After downloading and installing, you need to add a new domain to hMailServer. In my case I will not be using hMailServer to accept incoming email, hence I did not put the company’s email domain. Doing so will cause email to your colleague to be routed locally and likely fails.
So go ahead add a new domain, and just give it the local machine name (eg: devbox01.local). You have to pick a name that resembles an actual domain (with a dot and suffix), otherwise hMailServer will rejects it.
Once you’ve setup the domain, create a new account email@example.com
Set a password, and that’s it you’re done. You can now use the SMTP server for outbound email
- Username: firstname.lastname@example.org
- Password: whatever password you put in
- SMTP host: devbox01
- SMTP port: 25
Now what’s left to do is configuring firewall. If you program runs on the same box you might not need to do anything. However it’s good to check that no outside traffic from internet can connect to port 25 so no-one can abuse your SMTP server.
And as a last word of warning, do not assume all mails will be delivered
. This SMTP setup is very basic. Depending on the content you send, SPF
, reverse DNS entry, spam filtering of receipient, and gazillion other things, your email might not go through
SMTP server can be tested simply by using telnet client. On Windows 7 above telnet client has to be installed first via control panel (Windows Add/Remove Features)
Following is a sample SMTP commands to send for a standard mail server listening on IP 220.127.116.11 port 25, without authentication
C:\telnet 18.104.22.168 25
To: Gerry Tan <email@example.com>
From: My Company Support <firstname.lastname@example.org>
Subject: Testing mail server via SMTP
Please ignore this email as this is just testing mail server via SMTP
The two newlines and dot at the end is important.
SMTP Server With Authentication
To use SMTP username / password authentication, you first need to encrypt it to Base64. It can be done with command line perl:
perl -MMIME::Base64 -e 'print encode_base64("gerrytan");'
perl -MMIME::Base64 -e 'print encode_base64("Mypass123");'
Becareful if you username / password contains symbols meaningful to perl! An @ character can be interpreted as perl array. You have to escape it using \ (I spent an hour figuring out why authentication failed due to this).
And issue AUTH LOGIN command after HELO / EHLO. The server will prompt for username and password in Base 64
220 mail.tpg.com.au ESMTP (mail16) Sendmail ready.
250-mail16.tpgi.com.au Hello webmail.mxtglobal.com [22.214.171.124], pleased to
250-AUTH LOGIN PLAIN
235 2.0.0 OK Authenticated
250 2.1.0 email@example.com... Sender ok
RC250 2.1.5 firstname.lastname@example.org... Recipient ok
Thanks to http://exchange.mvps.org/smtp_frames.htm.
GMail provides a handy and reliable SMTP mail server for your program / script. Following are GMail SMTP configuration settings:
- SMTP Host Name: smtp.gmail.com
- SMTP Port: 587
- TLS Enabled: Yes
- Username: <your GMail email>
- Password: <your gmail password>
In Java you can use commons-email to simply send an E-Mail using your GMail account.
First add commons-email jar into your classpath. If you use Maven, simply add following dependency (or newer version if any):
Following example assumes your GMail email is email@example.com and password abcd1234.
Email email = new SimpleEmail();
email.setFrom("firstname.lastname@example.org", "John Doe");
email.setSubject("Hi this is testing email only");
email.setMsg("Hello there testing to send email from GMail");
Note that by default GMail only allows email to be sent from your address (email@example.com), you cannot send as somebody else for security reason. You need to perform additional configuration to allow external email to be sent via your GMail account.
If you own a domain name (for small business or so), firstname.lastname@example.org email will look more professional than email@example.com. However you might be annoyed having to check multiple mailboxes. Here’s how you can link everything into one GMail mailbox.
Setup Incoming Mail Forwarding On Your Domain Hosting Service
- Login into your domain hosting control panel. If you’re on GoDaddy, login to MyAccount and launch E-mail product
- Select Create Forward, enter firstname.lastname@example.org and forward it to email@example.com (you can even add multiple e-mails)
- Wait for a few minutes, and at this point you should get incoming mail forwarded to your gmail mailbox
Configure GMail For Sending As firstname.lastname@example.org
- Go to GMail setting (gear icon on the top right), select Accounts and Import
- Under Send Mail As section, select Add Another Email You Own, follow the prompt and verification
- When sending email, don’t forget to change the From address to email@example.com