首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python:发送电子邮件时,总是在以下子句中被阻止: smtpserver = smtplib.SMTP("smtp.gmail.com",587)

Python:发送电子邮件时,总是在以下子句中被阻止: smtpserver = smtplib.SMTP("smtp.gmail.com",587)
EN

Stack Overflow用户
提问于 2012-08-16 14:39:39
回答 3查看 9.1K关注 0票数 4

我正在编写一个Python程序来发送电子邮件。但每次执行该子句时:

代码语言:javascript
复制
smtpserver = smtplib.SMTP("smtp.gmail.com",587)

它将在这里阻塞,并始终保持正在执行状态,没有任何提示和错误。我也不知道原因。有人能帮我吗?

代码如下: import smtplib

代码语言:javascript
复制
to = 'toemail@gmail.com'
gmail_user = 'user@gmail.com'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + '\n this is test msg from mkyong.com \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()
EN

回答 3

Stack Overflow用户

发布于 2016-09-16 01:11:22

也许我晚了4年,但这对我来说是有效的,也可能对其他人有帮助!

代码语言:javascript
复制
server = smtplib.SMTP("smtp.gmail.com", 587, None, 30)
票数 3
EN

Stack Overflow用户

发布于 2014-02-18 15:18:31

作为参考,cpython smtplib是阻塞的。也就是说,它会在连接时阻止GIL (即Python)。尽管声称GIL是在I/O上发布的,但它只在一些I/O上发布,而SMTP连接不是这样的。要使其异步,您需要将邮件发送到另一个进程或线程,具体取决于您的情况。

票数 1
EN

Stack Overflow用户

发布于 2020-10-14 19:21:48

我遇到了类似的问题,上面的答案对我都不起作用。我的问题是,因为我的smtp服务器使用SSL。我需要使用smtplib.SMTP_SSL而不是smtplib.SMTP。我只是在下面发布了我的完整工作代码。请确保在执行前更改了reqemail_config的值。

代码语言:javascript
复制
    req = {
        "email": "to@something.com",
        "subject": "new e-mail from python",
        "body": "Hi,\nI am a bot",
    }

    email_config = {
        "server": "smtp.orange.fr",
        "port": 465,
        "username": "username@orange.fr",
        "password": "mypassword",
        "email": "fromwhichemail@orange.fr",
    }
代码语言:javascript
复制
#!/usr/bin/env python3
import smtplib

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class Mail(object):
    def __init__(self, email_config):
        self.email = email_config["email"]
        self.password = email_config["password"]
        self.server = email_config["server"]
        self.port = email_config["port"]
        print(f"Logging to {self.server}:{self.port}")
        session = smtplib.SMTP_SSL(self.server, self.port)
        print(f"Calling ehlo")
        session.ehlo()
        print(f"Calling login")
        session.login(self.email, self.password)
        self.session = session

    def send_message(self, subject, to, body, filename):
        # Create a multipart message and set headers
        message = MIMEMultipart()
        message["From"] = self.email
        message["To"] = to
        message["Subject"] = subject
        message["Bcc"] = ""
        # Add body to email
        message.attach(MIMEText(body, "plain"))
        print(f"tot: {to}")
        print(f"subject: {subject}")
        print(f"body: {body}")

        if filename is not None:
            # Open PDF file in binary mode
            with open(filename, "rb") as attachment:
                part = MIMEBase("application", "octet-stream")
                part.set_payload(attachment.read())
            # Encode file in ASCII characters to send by email
            encoders.encode_base64(part)
            # Add header as key/value pair to attachment part
            part.add_header(
                "Content-Disposition",
                f"attachment; filename= {filename}",)
            # Add attachment to message and convert message to string
            message.attach(part)

        # Send e-mail
        print(f"Sending e-mail...")
        self.session.sendmail(self.email, to, message.as_string())

if __name__ == "__main__":
    req = {
        "email": "to@something.com",
        "subject": "new e-mail from python",
        "body": "Hi,\nI am a bot",
    }

    email_config = {
        "server": "smtp.orange.fr",
        "port": 465,
        "username": "username@orange.fr",
        "password": "mypassword",
        "email": "fromwhichemail@orange.fr",
    }
    m = Mail(email_config)
    if "pdf_file" in req:
        m.send_message(req["subject"], req["email"], req["body"], req["pdf_file"])
    else:
        m.send_message(req["subject"], req["email"], req["body"], None)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11981907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档