请用python给任意邮箱发送一封邮件
时间: 2023-07-21 12:02:45 浏览: 50
以下是使用Python发送邮件的示例代码:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(sender_email, sender_password, receiver_email, subject, message):
# 创建一个MIMEMultipart对象
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
# 添加邮件正文
msg.attach(MIMEText(message, 'plain'))
# 使用SMTP发送邮件
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(sender_email, sender_password)
server.send_message(msg)
# 设置发件人邮箱和密码
sender_email = 'your_sender_email@gmail.com'
sender_password = 'your_sender_password'
# 设置收件人邮箱、邮件标题和内容
receiver_email = 'receiver_email@gmail.com'
subject = 'Python邮件测试'
message = '这是一封使用Python发送的测试邮件。'
# 发送邮件
send_email(sender_email, sender_password, receiver_email, subject, message)
```
请注意,这段代码使用了Gmail SMTP服务器作为示例,如果你使用其他邮箱提供商,请根据相应的SMTP服务器信息进行修改。同时,请确保允许低安全性应用程序访问你的发件人邮箱,以便成功发送邮件。