Setting Up a Fake SMTP Server

  1. https://github.com/Nilhcem/FakeSMTP
  2. http://www.we3geeks.org/2015/01/09/setting-up-a-fake-smtp-server/

On my team’s various development boxes (which are almost always laptops and therefore not always connected to our corporate intranet and sometime not on any network), we have typically configured our ColdFusion and Railo application servers to use our internal mail servers. Many of our Web applications send user-composed, automated, or process-based email messages, in addition to security-related event notifications. We’ve been bitten a couple of times in developing this mail-related logic, with messages being generated and sent to real system users from our development servers which, as you can imagine, has significant potential for generating concern and/or confusion on the users’ part. This hasn’t happened recently, in part because we are collectively more careful and in part based on our approach to how email gets addressed depending on whether the app is running on a development, testing, or production server.

I’ve long believed that a safer approach would be to configure our non-production servers to use something other than a “real” mail server. I’m in the process of standing up a new development system for myself, and decided I was going to poke at this a bit. For this first foray, I wanted something

  • simple to set up, configure, start, and stop
  • free
  • preferably available cross-platform (my team develops on Mac OS, Linux, and Windows systems based on developer preference for platform)
  • completely local (because of the need to be able to run without being on a network)

A solution would need to support SMTP, as both the ColdFusion and Railo application servers are configured to talk to an SMTP server. Anything beyond that set of basic requirements would be a bonus.

After a bit of looking, I decided to use FakeSMTP. It ticks all of the above boxes, and is implemented as a single Java JAR compatible with Java 1.6 and above. It has a simple UI for watching activity and  “outbound” mail handed to it, and can easily be configured to store transmitted mail messages to disk as .eml files for review (making developing email-related content fairly straightforward).

I use a bash shell script to start and stop my Tomcat instances for ColdFusion and Railo, so I added logic to that script to optionally start and stop my local mail server, as follows (wrapped here only for presentation):

# Start our fake SMTP server:
if [ "${3}" = "mail" ]; then
  java -jar ~/opt/fakesmtp/fakesmtp-1.13.jar -s -b -p 2525 \
    -o ~/opt/fakesmtp/out/ -a 127.0.0.1 \
    >> ~/opt/fakesmtp/logs/fakesmtp-`date +%Y%m%d`.log &
fi

and

# Stop our fake SMTP server:
if [ "${3}" = "mail" ]; then
  kill `ps -a > /tmp/ps.out && grep -i fakesmtp /tmp/ps.out | \
    cut -f 2 -d ' ' -`
fi

The start logic runs the mail server in the background (as I really don’t intend to use the UI), listening on address 127.0.0.1 and port 2525 (to avoid needing to run with elevated privileges on the default SMTP port of 25). It dumps mail messages to a folder for review, and logging server activity to a log file with a date-based name. The stop logic finds the mail server process and kills it.

I’ve tested it with both ColdFusion and Railo application servers, and it works. I will probably add some shutdown-related logic to do a bit of cleanup to make sure neither the log files or the saved email files get out of control.

Final thought: If it weren’t for my desire to be able to run completely standalone at times where I either do not have access to or do not want to use a network connection, I probably would have gone with something like Debug Mail. I have not used it, but it looks excellent. Given Railo’s ability to configure multiple mail servers, I may still play with it a bit as part of my solution.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值