24
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

telnetを使ってメールサーバーと通信する (SMTP/POP/IMAP)

Last updated at Posted at 2015-07-30

SMTPでメールを送る (認証ありPLAIN TEXT)

認証のための準備

SMTPの認証には以下の文字列をbase64化したものを使用する

%%アカウント%%\0%%アカウント%%\0%%パスワード

base64の文字列は以下のようにして変換する (一例)

$ echo -en "local1@example.com\0local1@example.com\0password" |base64
bG9jYWwxQGV4YW1wbGUuY29tAGxvY2FsMUBleGFtcGxlLmNvbQBwYXNzd29yZA==

概要

$ telnet %%SMTPサーバーのIPアドレス%% 587
EHLO %%SMTPサーバーのホスト名%%
AUTH PLAIN %%base64化した文字列%%
MAIL FROM:<%%送信元アドレス%%>
RCPT TO:<%%宛先%%>
DATA
From: %%送信元アドレス%%
Subject: %%件名%%

%% メール本文 %%
.
QUIT

メールヘッダと本文の間に1行空け、本文の終了に"."のみの行を置きます

実行例

$ telnet 192.168.3.71 587
Trying 192.168.3.71...
Connected to 192.168.3.71.
Escape character is '^]'.
220 test1.example.com OK
EHLO test1.example.com
250-Ok
250-SIZE 52428800
250-AUTH LOGIN PLAIN
250 8BITMIME
AUTH PLAIN bG9jYWwxQGV4YW1wbGUuY29tAGxvY2FsMUBleGFtcGxlLmNvbQBwYXNzd29yZA==
235 Authentication OK
MAIL FROM:<sender@example.com>
250 Ok
RCPT TO:<receiver@example.com>
250 Ok
DATA
354 Start mail input; end with <CRLF>.<CRLF>
From: sender@example.com
Subject: no subject

mail from you
.
250 Ok
QUIT
221 Goodbye
Connection closed by foreign host.

SMTPでメールを送る (認証なし)

上のパターンからAUTH PLAINを省いて、ポート番号が25に変わります

※通常のSMTPサーバーでは、もはや認証なしでは送信できないケースがほとんどです
契約中のプロバイダーが用意するメールサーバーを使う場合か、自分でLAN内に建てたサーバーを使う場合でのみ使える方法になると思います

概要

$ telnet %%SMTPサーバーのIPアドレス%% 25
EHLO %%SMTPサーバーのホスト名%%
MAIL FROM:<%%送信元アドレス%%>
RCPT TO:<%%宛先%%>
DATA
From: %%送信元アドレス%%
Subject: %%件名%%

%% メール本文 %%
.
QUIT

メールヘッダと本文の間に1行空け、本文の終了に"."のみの行を置きます

POPでメールを開く

概要

$ telnet %%POPサーバーのIPアドレス%% 110
USER %%アカウント%%
PASS %%パスワード%%
LIST
RETR %%メッセージ番号%%

LISTコマンドで受信しているメールのメッセージ番号を確認する

実行例

$ telnet 192.168.3.71 110
Trying 192.168.3.71...
Connected to 192.168.3.71.
Escape character is '^]'.
+OK Dovecot ready
USER user1@domain1.example
+OK
PASS password
+OK Logged in.
LIST
+OK 1 messages:
1 391
.
RETR 1
+OK 391 octets
Return-Path: <local1@example.com>
Received: from xxxxxx ([192.168.3.71])
        by test1.example id 55b9d0c8000e485400002a9900002a9c000000000000;
        Thu, 30 Jul 2015 16:22:48 +0900
From: local1@example.com
Subject: no subject
Message-Id: <55b9d0c8000e485400002a9900002a9c000000000000@test1.example>
Date: Thu, 30 Jul 2015 16:22:48 +0900

mail from you
.
QUIT
+OK Logging out.
Connection closed by foreign host.

IMAPでメールを開く

概要

$ telnet %%IMAPサーバーのIPアドレス%% 143
1 login "%%アカウント%%" "%%パスワード%%"
2 list "" *
3 select INBOX
4 fetch 1 body[]
5 logout

listコマンドでディレクトリの一覧を表示
selectコマンドでディレクトリの指定
fetchコマンドでメールを取得する

実行例

$ telnet 192.168.3.71 143
Trying 192.168.3.71...
Connected to 192.168.3.71.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE AUTH=PLAIN AUTH=LOGIN AUTH=DIGEST-MD5 AUTH=CRAM-MD5] Dovecot ready
1 login user1@domain1.example password
1 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS MULTIAPPEND UNSELECT IDLE CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS] Logged in
2 list "" *
* LIST (\HasNoChildren) "/" "Draft"
* LIST (\HasNoChildren) "/" "Sent"
* LIST (\HasNoChildren) "/" "Trash"
* LIST (\HasNoChildren) "/" "INBOX"
2 OK List completed.
3 select INBOX
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
* 1 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1437635449] UIDs valid
* OK [UIDNEXT 2] Predicted next UID
* OK [HIGHESTMODSEQ 1] Highest
3 OK [READ-WRITE] Select completed.
4 fetch 1 body[]
* 1 FETCH (BODY[] {391}
Return-Path: <local1@example.com>
Received: from xxxxxx ([192.168.3.71])
        by xxxxxx id 55b9d0c8000e485400002a9900002a9c000000000000;
        Thu, 30 Jul 2015 16:22:48 +0900
From: local1@example.com
Subject: no subject
Message-Id: <55b9d0c8000e485400002a9900002a9c000000000000@test1.example>
Date: Thu, 30 Jul 2015 16:22:48 +0900

mail from you
)
4 OK Fetch completed.
5 logout
* BYE Logging out
5 OK Logout completed.
Connection closed by foreign host.
24
31
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
24
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?