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
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.
E-mail (and internet) was invented long time ago with the assumption only very few people will do evil, but it’s not the case nowadays. It is possible to send e-mail to anyone posing as anyone else (ie: if you own the domain apple.com, you can send email as banana.com).
SPF stands for Sender Policy Framework. Long story short, it is set on the DNS zone record to configure What host is allowed to send email as your domain
Testing If Your Domain Is Setup Properly
The openspf website has list of tools you can use to check if your domain already has SPF setup properly. For example:
- Go to http://www.openspf.org/Why
- Enter the email address you’re sending as on MAIL FROM field and your smtp server under Sender's IP address (eg: smtp.apple.com)
- The tool was originally designed to debug rejection.. so although it passes it will say “your mail server rejected a message because”. If you read further, if your SPF record was correct it should say The domain mycompany.com has authorized to send mail on its behalf, so the message should have been accepted. It is impossible for us to say why it was rejected., whereas if it’s incorrect it will say The domain mycompany.com has not published an SPF policy. It is possible that the receiving mail server refuses all mail from domains that do not have an SPF policy.
Configuring SPF Record on Your DNS Zone
This can be done on your domain hosting. Add following DNS record to you domain name (eg: mycompany.com)
v=spf1 a mx ?all
This syntax basically says:
- Authorize the IP specified by A record as outbound mailer
- Authorize the IP specified by MX record as outbound mailer
- Mark everything else as Neutral