我是编程的新手。最近,我试图学习如何在Windows 10上使用Python发送电子邮件,但是出现了一些问题,我不知道此错误是什么意思。 有人可以帮我吗? 这是我的代码:
from email.mime.text import MIMEText
import smtplib
msg=MIMEText("<h1>a heading</h1><p>hellothere!</p>","html")
msg['subject']='a test html message'
msg['from']='<mohammedbehjooarchich@gmail.com>'
msg['to']='<mohammedbehjooarchich@gmail.com>'
s=smtplib.SMTP('127.0.0.1')
s.sendmail('<mohammedbehjooarchich@gmail.com',['<mohammedbehjooarchich@gmail.com'],msg.as_string())
print('message sent')
答案 0 :(得分:0)
这是我的代码希望对您有帮助:
这是为gmail设置的。
在这一部分中,您将需要替换信息。
USERNAME =“ ___您的SMTP电子邮件在这里___ ” ## gmail电子邮件地址 PASSWORD =“ __您的SMTP密码此处___ ” ## gmail电子邮件密码
在代码底部。
sendMail([“ _________接收邮件__ ”],##设置目标电子邮件地址。
代码:
#!/usr/bin/env python
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
USERNAME = "___YOUR SMTP EMAIL HERE___"
PASSWORD = "__YOUR SMTP PASSWORD HERE___"
def sendMail(to, subject, text, files=[]):
assert type(to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = USERNAME
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, to, msg.as_string())
server.quit()
sendMail( ["___EMAIL TO RECEIVE THE MESSAGE__"],
"Alarm notification",
"Someone has entered the room, picture attached",
["/home/pi/webcam.jpg"] )