python发送谷歌邮件
时间: 2023-07-07 07:06:30 浏览: 80
要使用 Python 发送谷歌邮件,需要使用 `smtplib` 和 `email` 库。下面是一个简单的示例代码,可以用来发送邮件:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 邮件配置
sender_email = "your_email@gmail.com"
receiver_email = "recipient_email@gmail.com"
password = "your_password"
subject = "Test Email"
body = "This is a test email sent using Python!"
# 创建邮件对象
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = receiver_email
msg["Subject"] = subject
# 添加邮件正文
msg.attach(MIMEText(body, "plain"))
# 发送邮件
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
print("Email sent successfully!")
```
在上面的示例代码中,需要将 `sender_email`,`receiver_email` 和 `password` 替换为你自己的谷歌邮箱地址和密码。此外,需要启用谷歌邮箱的“允许 less secure apps”选项,才能成功发送邮件。
阅读全文