用python写一个邮件发送功能
时间: 2023-07-01 19:07:33 浏览: 63
可以使用Python内置的smtplib库来发送邮件。以下是一个简单的示例:
```python
import smtplib
from email.mime.text import MIMEText
# 邮件设置
sender = 'your_email@example.com' # 发件人邮箱
password = 'your_password' # 发件人邮箱密码
receivers = ['recipient1@example.com', 'recipient2@example.com'] # 收件人邮箱,可以是多个
# 构建邮件内容
message = MIMEText('邮件内容', 'plain', 'utf-8')
message['From'] = sender
message['To'] = ','.join(receivers)
message['Subject'] = '邮件主题'
# 发送邮件
try:
smtp_obj = smtplib.SMTP('smtp.example.com', 25) # 邮件服务器地址和端口号
smtp_obj.login(sender, password) # 登录发件人邮箱
smtp_obj.sendmail(sender, receivers, message.as_string()) # 发送邮件
print('邮件发送成功')
except smtplib.SMTPException as e:
print('邮件发送失败:', e)
```
需要将代码中的`your_email@example.com`和`your_password`替换为真实的发件人邮箱和密码,并将`smtp.example.com`替换为实际的邮件服务器地址和端口号。`receivers`变量可以设置为一个包含多个收件人邮箱的列表。