免责声明:由于这个问题的广泛性(见下文;-),我对标题犹豫不决,其他选项包括:
首先,一点上下文:
为了学习,我正在构建一个具有用户注册功能的网站.这个想法是,注册后用户将收到一封带有激活链接的电子邮件.我想编写并发送来自Python代码的电子邮件,这是我想要求澄清的部分.
我的理解,在我开始之前(显然天真=)可以这样说明(假设有一个合法的电子邮件地址user@example.com):
搜索的例子后,我碰上#1(的一些问题与解答1,2,3,4).从这些我已经提炼出以下片段,撰写并发送来自Python代码的电子邮件:
import smtplib
from email.message import EmailMessage
message = EmailMessage()
message.set_content('Message content here')
message['Subject'] = 'Your subject here'
message['From'] = 'me@example.com'
message['To'] = 'user@example.com'
smtp_server = smtplib.SMTP('smtp.server.address:587')
smtp_server.send_message(message)
smtp_server.quit()
Run Code Online (Sandbox Code Playgroud)
接下来(明显的)问题是要传递给什么smtplib.SMTP()而不是'smtp.server.address:587'.从评论到这个答案,我发现本地SMTP服务器(仅用于测试目的)可以通过python3 -m smtpd -c DebuggingServer -n localhost:1025,然后smtp_server = smtplib.SMTP('smtp.server.address:587')可以更改为,smtp_server = smtplib.SMTP('localhost:1025')并且所有已发送的电子邮件将显示在控制台中(从python3 -m smtpd -c DebuggingServer -n localhost:1025 …
嗨,我想通过smtp协议发送邮件到我的一个Gmail帐户...
我试过但最后发生了错误:
telnet> open alt4.gmail-smtp-in.l.google.com 25
Trying 74.125.131.27...
Connected to alt4.gmail-smtp-in.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP b4si2095585vdw.57 - gsmtp
HELO stackoverflow.com
250 mx.google.com at your service
MAIL FROM: <test@stackoverflow.com>
250 2.1.0 OK b4si2095585vdw.57 - gsmtp
RCPT TO: <????@gmail.com> // filtered ;)
250 2.1.5 OK b4si2095585vdw.57 - gsmtp
DATA
354 Go ahead b4si2095585vdw.57 - gsmtp
test
ok
it done
.
550-5.7.1 [5.22.81.102] The IP you're using to send mail is not authorized to
550-5.7.1 send email directly to our …Run Code Online (Sandbox Code Playgroud)