smtplib
— SMTP (简单邮件传输协议) 客户端
¶
源代码: Lib/smtplib.py
The
smtplib
module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon. For details of SMTP and ESMTP operation, consult
RFC 821
(简单邮件传输协议) 和
RFC 1869
(SMTP 服务扩展)。
可用性 :非 WASI。
This module does not work or is not available on WebAssembly. See WebAssembly 平台 了解更多信息。
- class smtplib. SMTP ( host='' , port=0 , local_hostname=None , [ timeout , ] source_address=None ) ¶
-
An
SMTPinstance encapsulates an SMTP connection. It has methods that support a full repertoire of SMTP and ESMTP operations. If the optional host and port parameters are given, the SMTPconnect()方法被调用采用这些参数在初始化期间。若指定, local_hostname 在 HELO/EHLO 命令中被用作本地主机的 FQDN (完全合格域名)。否则,查找本地主机名使用socket.getfqdn()。若connect()调用返回任何内容除成功代码外,SMTPConnectError被引发。可选 timeout 参数指定超时 (以秒为单位) 为阻塞像连接尝试操作 (若未指定,将使用全局默认超时设置)。若超时到期,TimeoutError被引发。可选 source_address parameter allows binding to some specific source address in a machine with multiple network interfaces, and/or to some specific source TCP port. It takes a 2-tuple(host, port), for the socket to bind to as its source address before connecting. If omitted (or if host or port are''and/or0respectively) the OS default behavior will be used.对于正常使用,应该仅要求初始化/连接,
sendmail(),和SMTP.quit()方法。包括下文范例。The
SMTP类支持with语句。当像这样使用时,SMTPQUIT命令被自动发出当with语句退出。如:>>> from smtplib import SMTP >>> with SMTP("domain.org") as smtp: ... smtp.noop() ... (250, b'Ok') >>>