通过 SMTP 发送电子邮件时遇到问题 Python
Having trouble with sending an email through SMTP Python
所以我正尝试使用 Python 通过 SMTPlib 发送电子邮件,但我无法让它工作。我阅读了 Microsoft SMTP 规范,并相应地将它们放入其中,但我无法让它工作。这是我的代码:
# Send an email
SERVER = "smtp-mail.outlook.com"
PORT = 587
USER = "******@outlook.com"
PASS = "myPassWouldBeHere"
FROM = USER
TO = ["******@gmail.com"]
SUBJECT = "Test"
MESSAGE = "Test"
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, MESSAGE)
try:
server = smtplib.SMTP()
server.connect(SERVER, PORT)
server.starttls()
server.login(USER,PASS)
server.sendmail(FROM, TO, message)
server.quit()
except Exception as e:
print e
print "\nCouldn't connect."
我从键盘记录器那里得到了代码,但我稍微清理了一下。我阅读了 here 基本 SMTP 的工作原理,但是有一些东西像 starttls
(方法)我不太明白。
非常感谢您对此提供的帮助。
试试这个。这适用于 Python 2.7.
def send_mail(recipient, subject, message):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
username = "sender@outlook.com"
password = "sender's password"
msg = MIMEMultipart()
msg['From'] = username
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText(message))
try:
print('sending mail to ' + recipient + ' on ' + subject)
mailServer = smtplib.SMTP('smtp-mail.outlook.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(username, password)
mailServer.sendmail(username, recipient, msg.as_string())
mailServer.close()
except error as e:
print(str(e))
send_mail('recipient@example.com', 'Sent using Python', 'May the force be with you.')
所以我正尝试使用 Python 通过 SMTPlib 发送电子邮件,但我无法让它工作。我阅读了 Microsoft SMTP 规范,并相应地将它们放入其中,但我无法让它工作。这是我的代码:
# Send an email
SERVER = "smtp-mail.outlook.com"
PORT = 587
USER = "******@outlook.com"
PASS = "myPassWouldBeHere"
FROM = USER
TO = ["******@gmail.com"]
SUBJECT = "Test"
MESSAGE = "Test"
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, MESSAGE)
try:
server = smtplib.SMTP()
server.connect(SERVER, PORT)
server.starttls()
server.login(USER,PASS)
server.sendmail(FROM, TO, message)
server.quit()
except Exception as e:
print e
print "\nCouldn't connect."
我从键盘记录器那里得到了代码,但我稍微清理了一下。我阅读了 here 基本 SMTP 的工作原理,但是有一些东西像 starttls
(方法)我不太明白。
非常感谢您对此提供的帮助。
试试这个。这适用于 Python 2.7.
def send_mail(recipient, subject, message):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
username = "sender@outlook.com"
password = "sender's password"
msg = MIMEMultipart()
msg['From'] = username
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText(message))
try:
print('sending mail to ' + recipient + ' on ' + subject)
mailServer = smtplib.SMTP('smtp-mail.outlook.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(username, password)
mailServer.sendmail(username, recipient, msg.as_string())
mailServer.close()
except error as e:
print(str(e))
send_mail('recipient@example.com', 'Sent using Python', 'May the force be with you.')