telnet mail server (pop3, imap, smtp)

最後更新: 2024-02-16

目錄

 


imap

 

 * RFC 3501

CAPABILITY

telnet 後會自到基本的 CAPABILITY, 比如 STARTTLS, AUTH=X

* OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ STARTTLS AUTH=PLAIN AUTH=LOGIN] Dovecot (Ubuntu) ready.

* Login 後才可以看到是否有其他的 CAPABILITY.

   e.g. QRESYNC and CONDSTORE

Login & Logout

1 login user_name password

1 OK [CAPABILITY IMAP4rev1 ... ] Logged in

 * Login 前及 Login 後係有不同的 CAPABILITY

. logout

查看有什麼 Folder

Syntax

. list MAILBOX FILTER

"" A non-empty reference name argument is the name of a mailbox or a level of mailbox hierarchy

"*" is a wildcard, and matches zero or more characters at this position.

2 list "" "*"

2 list "" "*"
* LIST (\HasNoChildren) "." "Sent"
* LIST (\HasChildren) "." "Backup"
* LIST (\HasNoChildren) "." "Backup.Folder2"
* LIST (\HasNoChildren) "." "Backup.Folder1"
* LIST (\HasNoChildren) "." "Trash"
* LIST (\HasNoChildren) "." "Junk"
* LIST (\HasNoChildren) "." "Drafts"
* LIST (\HasNoChildren) "." "INBOX"
2 OK List completed.

查看某 Folder 內的 sub-folder

3 list "Backup" "*"

* LIST (\HasChildren) "." "Backup"
* LIST (\HasNoChildren) "." "Backup.Folder2"
* LIST (\HasNoChildren) "." "Backup.Folder1"
3 OK List completed.

查看某 Folder 資訊

獲得 The number of messages

Syntax

. status MAILBOX (STATUS)

STATUS: messages / unseen / recent / uidvalidity

e.g.

. status INBOX (messages)

* STATUS "INBOX" (MESSAGES 2)
. OK Status completed.

. status INBOX (unseen)

* STATUS "INBOX" (UNSEEN 1)
. OK Status completed.

建立, 刪除, rename Folder

. create INBOX.test1

. rename INBOX.test1 INBOX.test2

. delete INBOX.test2

進入某 Folder

To selects a mailbox so that messages in the mailbox can be accessed

The server MUST send the following untagged data to the client.

FLAGS, EXISTS, RECENT, UNSEEN, PERMANENTFLAGS, UIDNEXT, UIDVALIDITY

e.g.

. create INBOX.test3

. select INBOX.test3

. select INBOX

Subscribe

# 查看 Subscribe 了什麼 Folder

. lsub "" "*"

# "create" 的 Folder 後不會自動 Subscribe

. subscribe INBOX.test3

. unsubscribe INBOX.test3

Search messages

Syntax

. SEARCH KEY [KEY2]

 * The matching(key) is case-insensitive

e.g.

. SEARCH FROM "[email protected]"

. SEARCH FROM "[email protected]" SINCE 4-May-2017

The defined search keys:

  • FROM string
  • TO string
  • CC string
  • SUBJECT string
  • BODY string
    -----------
  • BEFORE date
  • SINCE date
    -----------
  • HEADER field-name string
  • UID sequence set
    -----------
  • FLAGGED
  • NEW                       # Messages that have the \Recent flag set but not the \Seen
  • OLD                        # Messages that not have the \Recent flag set
  • KEYWORD <flag>
  • ...

Fetching messages

  • ALL                     # 它是 Macro 來, 相當於 (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE)
  • FAST                   # 它是 Macro 來, 相當於 (FLAGS INTERNALDATE RFC822.SIZE)
  • BODY[<section>]<<partial>>
  • INTERNALDATE     # The internal date of the message
  • ENVELOPE           # This is computed by the server by parsing the [RFC-2822] header
  • FLAGS                 # Flags set for the message
  • UID                     # The UID of the message (N)

BODY[<section>]<<partial>>

  • BODY[TEXT]        # Just the mail body, without the headers
  • BODY[HEADER]    # The mail headers

 

e.g.

. fetch 1 internaldate

* 1 FETCH (INTERNALDATE "17-Jul-2024 17:02:32 +0800")

. FETCH 817 FLAGS

. 817 FETCH (FLAGS (\Answered \Deleted \Seen))

Set flag

# [Un-]Set

a STORE 22 +FLAGS \Deleted   set as deleted email 22 (twenty-second email)

a STORE 22 -FLAGS \Deleted   restore, undelete ... email n. 22 in mailbox

# Bulk set flag

a STORE 1:* +FLAGS \Seen     set read all emails
a STORE 1:* -FLAGS \Seen     set unread all emails
a STORE 10:100 +FLAGS \Seen  set as read emails from 10 to 100(sequence number range)

 * 所有數字都可以用 "." 代替

其他:

. capability

. getquotaroot inbox

. getacl inbox

Remark:

IMAP has no built in move command, when you move a message you actually copy it to another folder and then delete the original.

imap 成功 login 後不會 move new/ 內的 file 到 cur/

 


POP3

 

USER userid

+OK

PASS password

+OK Logged in.

STAT

# Returns total number of messages and total size

+OK 4 1196

LIST

# Lists all messages: Returns indexed list of messages, along with size

+OK 4 messages:
1 299
2 299
3 299
4 299
.

RETR N

Return-Path: <s@a>
X-Original-To: r@b
Delivered-To: r@b
Received: from FromServer (x.ctinets.com [x.x.x.x])
        by server (Postfix) with SMTP id 9765D40D73DE
        for <r@b>; Wed, 26 Oct 2016 10:43:48 +0800 (HKT)

test msg
.

DELE N

+OK Marked to be deleted.

RSET

resets any messages previously marked for deletion in this session so that the QUIT command will not delete them.

TOP

Retrieving part of the message

# Return headers only:

TOP 1 0

# Return headers and first 10 lines of body:

TOP 1 10

QUIT

+OK Logging out, messages deleted.

ID 與 mail 的對應:

uidl

+OK
1 00000001581017bb
2 00000002581017bb
3 00000003581017bb
4 00000004581017bb

Remark

pop3 成功 login 後, 所有在 new/ 內的 file (不論是否 mail) 都會移到 cur/


smtp test login

 

# gen login name

perl -MMIME::Base64 -e 'print encode_base64("datahunter\@datahunter.org");'

# gen password

perl -MMIME::Base64 -e 'print encode_base64("password");'

測試:

telnet <mail_server> 25

# EHLO 在 postfix 上是沒有 log 的

C: EHLO mailserver.com

C: AUTH LOGIN

S: 334 VXNlcm5hbWU6;

C: <早前 gen 的 login name>

S: Now the server should have returned 334 UGFzc3dvcmQ6;

C: <早前gen password>

# login 成功了後都是沒有 log 的

S: 235 2.7.0 Authentication successful

# login fail 才有 log 內有 "warning: unknown[192.168.123.200]: SASL LOGIN authentication failed: UGFzc3dvcmQ6"

# 成功的 log 要交了 "mail from:" 及 "rcpt to:" 後才出

0311F3019: client=unknown[x.x.x.x], sasl_method=LOGIN, [email protected]

 

進階

Mail Body

Message-ID: <1>
Date: Fri, 23 Feb 2024 14:48:28 +0800
To: a@a
From: b@b
Subject: test

test msg

沒有 Message-ID 及 Date 很易被當成 SPAM

其他 mail header

Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

 

Creative Commons license icon Creative Commons license icon