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 firstname.lastname@example.org
Set a password, and that’s it you’re done. You can now use the SMTP server for outbound email
- Username: email@example.com
- 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 188.8.131.52 port 25, without authentication
C:\telnet 184.108.40.206 25
To: Gerry Tan <firstname.lastname@example.org>
From: My Company Support <email@example.com>
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 [220.127.116.11], pleased to
250-AUTH LOGIN PLAIN
235 2.0.0 OK Authenticated
250 2.1.0 firstname.lastname@example.org... Sender ok
RC250 2.1.5 email@example.com... 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 firstname.lastname@example.org and password abcd1234.
Email email = new SimpleEmail();
email.setFrom("email@example.com", "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 (firstname.lastname@example.org), 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), email@example.com email will look more professional than firstname.lastname@example.org. 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 email@example.com and forward it to firstname.lastname@example.org (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 email@example.com
- 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 firstname.lastname@example.org