利用Python实现发邮件功能
时间: 2024-03-21 07:19:32 浏览: 54
python实现邮件循环自动发件功能
以下是一个Python程序,可以通过SMTP协议发送电子邮件。
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 邮件发送者和接收者
sender = 'your_email@example.com'
receiver = 'recipient_email@example.com'
# 邮件主题
subject = 'Python SMTP 邮件测试'
# 邮件正文
content = '这是一封测试邮件,请勿回复。'
# 创建邮件正文对象
message = MIMEText(content, 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(receiver, 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
# 发送邮件
try:
smtp_obj = smtplib.SMTP('smtp.example.com')
smtp_obj.login('your_email@example.com', 'your_email_password')
smtp_obj.sendmail(sender, receiver, message.as_string())
print('邮件发送成功')
except smtplib.SMTPException as e:
print('邮件发送失败:', e)
```
请将`your_email@example.com`和`recipient_email@example.com`替换为实际的电子邮件地址,将`smtp.example.com`替换为实际的SMTP服务器地址,将`your_email_password`替换为实际的电子邮件账户密码。
阅读全文