Python催发邮件
时间: 2024-05-16 10:10:41 浏览: 68
Python可以使用smtplib和email库来发送邮件。首先,需要使用smtplib库建立与SMTP服务器的连接,并登录该服务器。然后,使用email库创建邮件内容,并发送邮件。
以下是一个简单的示例代码:
```
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 发送邮件的邮箱和密码
sender = 'sender@example.com'
password = 'password'
# 收件人邮箱
receiver = 'receiver@example.com'
# 邮件内容
message = MIMEText('这是一封测试邮件', 'plain', 'utf-8')
message['From'] = Header('发件人名称', 'utf-8')
message['To'] = Header('收件人名称', 'utf-8')
message['Subject'] = Header('测试邮件', 'utf-8')
# 发送邮件
try:
smtpObj = smtplib.SMTP('smtp.example.com', 25)
smtpObj.login(sender, password)
smtpObj.sendmail(sender, receiver, message.as_string())
print('邮件发送成功')
except Exception as e:
print('邮件发送失败:', e)
finally:
smtpObj.quit()
```
在上面的示例代码中,需要将`sender`和`password`替换为发送邮件的邮箱和密码,将`receiver`替换为收件人邮箱,将`smtp.example.com`替换为SMTP服务器地址,`25`替换为SMTP服务器端口号。同时,需要将邮件内容进行相应的修改。
阅读全文
相关推荐










