简体   繁体   English

python编程发送电子邮件 - ConnectionRefusedError: [WinError 10061] 无法建立连接

[英]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.

相关问题 VS-Code Python 调试 - ConnectionRefusedError: [WinError 10061] 由于目标机器主动拒绝,无法建立连接 - VS-Code Python debugging - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it WinError 10061 - 无法建立连接 - WinError 10061 - No Connection Could be made ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它 - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it PyThon: winrm [WinError 10061] 由于目标机器主动拒绝,无法建立连接')) - PyThon: winrm [WinError 10061] No connection could be made because the target machine actively refused it')) 当 ConnectionRefusedError: [WinError 10061] 时重试 - Retry when ConnectionRefusedError: [WinError 10061] python连接到mySql error-ConnectionRefusedError:[WinError 10061] - python connect to mySql error-ConnectionRefusedError: [WinError 10061] ConnectionRefusedError:[WinError 10061] 在 Python 3.5 中使用 Selenium 时 - ConnectionRefusedError: [WinError 10061] while using Selenium in Python 3.5 BrowserMobProxy: ConnectionRefusedError: [WinError 10061] 系统重启后 - BrowserMobProxy: ConnectionRefusedError: [WinError 10061] after system restart Python套接字编程ConnectionRefusedError:[Errno 61]连接被拒绝 - Python Socket Programming ConnectionRefusedError: [Errno 61] Connection refused Ftplib ConnectionRefusedError: [Errno 111] 连接被拒绝 (python 3.5) - Ftplib ConnectionRefusedError: [Errno 111] Connection refused (python 3.5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM