You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCharm中无法找到smtplib包,Python 3.x如何实现邮件发送?

Hey there! Let me clear this up first: smtplib isn't a third-party package you need to install — it's part of Python's standard library, which means it comes pre-installed with every Python 3.x distribution. That's why you can't find it in PyCharm's package list!

How to Send Emails with smtplib in Python 3.x

No need to hunt for packages — you can start using smtplib right away. Here's a step-by-step guide to sending emails:

Step 1: Import Required Modules

You'll need smtplib for handling the SMTP connection, plus email.mime.text to structure your email properly:

import smtplib
from email.mime.text import MIMEText

Step 2: Configure Your Email Provider Settings

Most email services (like Gmail, Outlook) require an app-specific password (not your regular account password) and use specific SMTP servers/ports. Here's a Gmail example:

# Core email details
sender_email = "your_email@gmail.com"
receiver_email = "recipient@example.com"
# Get this from your email provider's security settings (enable 2FA first for Gmail)
app_password = "your_app_specific_password"

# SMTP server config (adjust for your provider)
smtp_server = "smtp.gmail.com"
smtp_port = 587  # TLS encryption port (most common)

Step 3: Craft Your Email Message

Use MIMEText to set your email's subject and body:

# Create the message body
message = MIMEText("Hi there! This is a test email sent from Python using smtplib.")
# Add email headers
message["Subject"] = "Test Email from Python"
message["From"] = sender_email
message["To"] = receiver_email

Step 4: Send the Email

Set up a secure connection to the SMTP server and send your message:

try:
    # Use a context manager to handle the SMTP session automatically
    with smtplib.SMTP(smtp_server, smtp_port) as server:
        server.starttls()  # Enable encryption to secure the connection
        server.login(sender_email, app_password)
        # Send the email
        server.sendmail(sender_email, receiver_email, message.as_string())
    print("Email sent successfully! 🎉")
except Exception as e:
    print(f"Oops, something went wrong: {e}")

Key Tips to Avoid Issues

  • App-Specific Passwords: For Gmail, you must enable 2-step verification first, then generate an app password in your Google Account settings. Outlook, Yahoo, and other providers have similar processes.
  • Provider-Specific SMTP Details:
    • Outlook/Office 365: Server smtp.office365.com, port 587
    • Yahoo: Server smtp.mail.yahoo.com, port 587
  • PyCharm Interpreter Check: Make sure your PyCharm project is using a valid Python 3.x interpreter (go to File > Settings > Project: [Your Project Name] > Python Interpreter to confirm). Since smtplib is standard, it should already be listed under "Built-in" packages.

内容的提问来源于stack exchange,提问作者Pandyan Quest

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:49:25