python调用foxmail发邮件
时间: 2023-06-30 14:21:32 浏览: 199
python调用shell命令
可以使用Python中的smtplib和email库来发送邮件,以下是一个使用Foxmail发送邮件的示例代码:
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 发件人邮箱账号
sender = 'your_email_address'
# 发件人邮箱密码(注意不是邮箱登录密码,而是授权码)
password = 'your_email_password'
# 收件人邮箱账号
receiver = 'recipient_email_address'
# 邮件主题
subject = '邮件主题'
# 邮件正文内容
message = MIMEText('邮件正文', 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(receiver, 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
# 发送邮件
try:
smtpObj = smtplib.SMTP()
smtpObj.connect('smtp.qq.com', 587) # Foxmail邮箱smtp服务器地址和端口号
smtpObj.starttls() # 开启TLS加密传输
smtpObj.login(sender, password) # 登录邮箱
smtpObj.sendmail(sender, receiver, message.as_string()) # 发送邮件
smtpObj.quit() # 退出连接
print("邮件发送成功")
except smtplib.SMTPException:
print("邮件发送失败")
```
其中,`your_email_address`和`your_email_password`需要替换成你的Foxmail邮箱账号和授权码,`recipient_email_address`是收件人的邮箱账号,`邮件主题`和`邮件正文`也需要改成你要发送的内容。
阅读全文