hMailServer for Outbound Only SMTP Server

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.

New Domain

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.

New Account

Once you’ve setup the domain, create a new account noreply@devbox01.local

hmail

Set a password, and that’s it you’re done. You can now use the SMTP server for outbound email

  • Username: noreply@devbox01.local
  • Password: whatever password you put in
  • SMTP host: devbox01
  • SMTP port: 25

Important

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

 

Advertisements

Testing SMTP Server Using Telnet

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 12.23.34.45 port 25, without authentication

C:\telnet 12.23.34.45 25
HELO mycompany.com
MAIL FROM:support@mycompany.com
RCPT TO:gerry@somedomain.com
DATA
To: Gerry Tan <gerry@somedomain.com>
From: My Company Support <support@mycompany.com>
Subject: Testing mail server via SMTP
Please ignore this email as this is just testing mail server via SMTP

.
quit

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.
EHLO mail.tpg.com.au
250-mail16.tpgi.com.au Hello webmail.mxtglobal.com [220.245.123.18], pleased to
meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE 28521268
250-DSN
250-AUTH LOGIN PLAIN
250-STARTTLS
250-DELIVERBY
250 HELP
AUTH LOGIN
334 VXNlcm5hbWU6
**********
334 UGFzc3dvcmQ6
**********
235 2.0.0 OK Authenticated
MAIL FROM:gerry@email.com
250 2.1.0 gerry@email.com... Sender ok
RCPT TO:bob@email.com
RC250 2.1.5 bob@email.com... Recipient ok

Thanks to http://exchange.mvps.org/smtp_frames.htm.

Sending E-Mail Using GMail SMTP via Apache Commons Emails

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):

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-email</artifactId>
  <version>1.3.1</version>
</dependency>

Following example assumes your GMail email is johndoe@gmail.com and password abcd1234.

Email email = new SimpleEmail();
email.setSmtpPort(587);
email.setHostName("smtp.gmail.com");
email.setAuthentication("johndoe@gmail.com", "abcd1234");
email.setStartTLSEnabled(true);
email.setFrom("johndoe@gmail.com", "John Doe");
email.setSubject("Hi this is testing email only");
email.setMsg("Hello there testing to send email from GMail");
email.addTo("someone@outhere.com");
email.send();

Note that by default GMail only allows email to be sent from your address (johndoe@gmail.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.

Using GMail With Your Own Domain E-mail

If you own a domain name (for small business or so), myname@mydomain.com email will look more professional than myname@gmail.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

  1. Login into your domain hosting control panel. If you’re on GoDaddy, login to MyAccount and launch E-mail product
  2. Select Create Forward, enter myname@mydomain.com and forward it to myname@gmail.com (you can even add multiple e-mails)
  3. Wait for a few minutes, and at this point you should get incoming mail forwarded to your gmail mailbox

Configure GMail For Sending As myname@mydomain.com

  1. Go to GMail setting (gear icon on the top right), select Accounts and Import
  2. Under Send Mail As section, select Add Another Email You Own, follow the prompt and verification
  3. When sending email, don’t forget to change the From address to myname@mydomain.com