LoginSignup
34
34

ダミーSMTPサーバ(smtp4dev)

Last updated at Posted at 2019-12-19

はじめに

開発でメール送信のテストをするのに会社のメールサーバを使うのも良くないですし、間違ってお客さんのメールに送ってしまうという事故を防ぐためにダミーSMTPサーバがないかと思い調べたらsmtp4devというダミーSMTPサーバーがあったので使い方を説明する。
.NET Coreで作られているので、Windows、Linux、Mac OS-X(および.NET Coreを利用)で動作します。

image.png

概要

今回はWindows10にインストールしてから動作テストまでを説明します。
古いバージョンでは、Windows GUIベース(ここ)のがありますが、今回は、exe実行して、ダミーSMTPサービスを立ち上げて、ブラウザでメールの受信状況を確認できます。
https://github.com/rnwood/smtp4dev

インストール(WindowsPCで直接実行)

1.GitHubからプログラムをダウンロード

  • リンクはGithubのreleasesからダウンロード(ここ)

  • 今回はwindows10なのでx64ベースをダウンロード
    image.png

  • ダウンロードしたファイルを解凍

2.Rnwood.Smtp4dev.exeを実行

1.Smtp4dev.exeを実行
ダウンロードしたファイルを解凍して、中にあるRnwood.Smtp4dev.exe を実行する。
image.png

image.png
4.ブラウザでサーバが立ち上がっているか確認

3.動作テスト

1.PowerShell ISEを起動

2.下記のメール送信用のスクリプトをPowerShell ISEに貼り付け、実行ボタンを押す

param($count=1)

1..$count | %{ 
    write-host $_
    Send-MailMessage -To foo@bar.com -From from@from.com -Subject "Subject:メールタイトル $_" -Body "Body:メール本文" -Encoding "utf8" -SmtpServer localhost
}

image.png

3.Browserに下記のURLを貼り付けてメールが届いている確認できる
http://localhost:5000/
image.png

インストール(Dockerバージョン)

  • exeを起動するのに.net Coreのモジュールが必要でしたが、こちらはDockerをインストールしてコマンド実行するだけで環境を構築することができます。

1.Docker-composeファイルのダウンロード

製作者のサイトにdocker-compose.ymlのファイルが
あるのでダウンロードしてください。
下記は、不要なコメントを削除したものになります。今回は下記のdocker-compose.ymlを実行しました。

version: '3'

services:
  smtp4dev:
    image: rnwood/smtp4dev:v3
    restart: always
    ports:
      # Change the number before : to the port the web interface should be accessible on
      - '5000:80'
      # Change the number before : to the port the SMTP server should be accessible on
      - '25:25'
      # Change the number before : to the port the IMAP server should be accessible on
      - '143:143'
    volumes:
      # This is where smtp4dev stores the database..
        - smtp4dev-data:/smtp4dev
    environment:
      #Specifies the server hostname. Used in auto-generated TLS certificate if enabled.
      - ServerOptions__HostName=smtp4dev
volumes:
  smtp4dev-data:

2.Dockerコマンドの実行

  • Dockerをインストールしていれば、コマンドプロンプトからでも構いません。
    今回はVS Codeのターミナルから実行します。
docker-compose up
  • 下記は実行結果です。
    image.png

3.動作テスト

1.Powershell ISEから下記のコマンドを実行

param($count=1)

1..$count | %{ 
    write-host $_
    Send-MailMessage -To foo@bar.com -From from@from.com -Subject "Subject:メールタイトル $_" -Body "Body:メール本文" -Encoding "utf8" -SmtpServer localhost
}


image.png

2.Browserに下記のURLを貼り付けてメールが届いている確認できる
http://localhost:5000/
image.png

その他のダミーSMTPサーバ

SendGrid記事 メールのテストをする方法

34
34
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
34