Python脚本发送邮件不带附件
时间: 2024-12-07 22:11:55 浏览: 11
在Python中,你可以使用内置的`smtplib`库和`email`模块来发送纯文本邮件,而不需要附件。以下是一个简单的例子:
```python
import smtplib
from email.mime.text import MIMEText
# 邮件设置
sender_email = 'your_email@example.com'
receiver_email = 'recipient_email@example.com'
password = 'your_password' # 邮箱授权码
subject = 'Hello from Python'
body = 'This is a test email sent using Python.'
# 创建MIMEText对象
msg = MIMEText(body)
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
# 连接到SMTP服务器并发送邮件
smtp_server = 'smtp.example.com' # 根据你的邮箱提供商填写
smtp_port = 587 # 或者465(如果需要加密)
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() # 如果需要加密连接
server.login(sender_email, password)
server.send_message(msg)
print('Email sent successfully.')
阅读全文