[英]python programming to send an email - ConnectionRefusedError: [WinError 10061] No connection could be made
I'm new to programming .recently I was trying to learn how to send an email using Python on Windows 10.but there were some problems and I don't know what does this error mean.我是编程新手。最近我试图学习如何在 Windows 10 上使用 Python 发送电子邮件。但是出现了一些问题,我不知道这个错误是什么意思。 Could someone help me?
有人可以帮助我吗? here is my code :
这是我的代码:
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')
here's my code hope this helps:这是我的代码,希望这会有所帮助:
This is set for gmail.这是为 gmail 设置的。
In this part you will need to replace with you information.在这部分中,您将需要替换为您的信息。
USERNAME = " ___YOUR SMTP EMAIL HERE___ " ## gmail email address PASSWORD = " __YOUR SMTP PASSWORD HERE___ " ## gmail email password USERNAME = " ___YOUR SMTP EMAIL HERE___ " ## gmail 电子邮件地址 PASSWORD = " __YOUR SMTP PASSWORD HERE___ " ## gmail 电子邮件密码
in the bottom section of the code.在代码的底部。
sendMail( [" ___EMAIL TO RECEIVE THE MESSAGE__ "], ## set the destination email address. sendMail( [" ___EMAIL TO RECEIVE THE MESSAGE__ "], ## 设置目标电子邮件地址。
CODE:代码:
#!/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"] )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.