通过nc使用smtp命令发送邮件

# 首先转换用户名、密码字符串成base64位,后面交互认证时使用
>>> import base64
>>> base64.encodestring('sender@163.com')
'AXhhbXBsZV9jb21AMTYzLmNvbQ==n'
>>> base64.encodestring('password')
'ATEyMzQ1Ng==n'

[z@z ~]$ nc smtp.163.com 25
220 163.com Anti-spam GT for Coremail System (163com[20121016])
helo amosk.info # 告诉服务器客户端主机名
250 OK
auth login # 认证
334 dXNlcm5hbWU6
AXhhbXBsZV9jb21AMTYzLmNvbQ==
334 UGFzc3dvcmQ6
ATEyMzQ1Ng==
235 Authentication successful
mail from: <sender@163.com> # 发件人
250 Mail OK
rcpt to: <receiver@163.com> # 收件人
250 Mail OK
data # 通知服务器开始接受邮件内容
354 End data with .
To:receiver@163.com
From:sender@163.com
Subject: test subject

mail body
hello, world!

.   # '.' 代表邮件内容输入结束
250 Mail OK queued as smtp12,EMCowECpgLOcO4hTMpa4AA--.1548S2 1401437200
quit
221 Bye
^C
# 使用smtplib库发送邮件
import base64, smtplib

s = smtplib.SMTP('smtp.163.com')
s.set_debuglevel(1)
s.docmd('helo server')
s.docmd('auth login')
s.send(base64.encodestring('sender@163.com'))
s.getreply()
s.send(base64.encodestring('password'))
s.getreply()

s.docmd('mail from: <sender@163.com>')
s.docmd('rcpt to: <receiver@163.com>')
s.docmd('data')
msg = '''To:receiver@163.com
From:sender@163.com
Subject: test subject

hello, world!

'''
s.send(msg)
s.send(".rn")
s.getreply()
s.quit()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.