苦 smtplib 中如何使用 SMTP 协议发送粥的指令?
粥指令:
Subject:粥请求
From: your_name@example.com
To: restaurant@example.com
Cc: delivery@example.com
Body:
Hello, Restaurant!
Please prepare the following dishes:
* Chicken noodle soup
* Beef noodle soup
* Vegetable soup
Please let me know when the order is ready.
Thank you for your prompt attention to this matter.
Sincerely,
Your Name
步驟:
- 在您的 Python 脚本中添加以下代码:
import smtplib
# 您的 SMTP 服务器信息
server_address = "smtp.example.com"
server_port = 587
# 您的用户名和密码
username = "your_username"
password = "your_password"
# 构建邮件对象
message = """
Subject:粥请求
From: {}@example.com
To: {}@example.com
Cc: {}@example.com
Body:
Hello, Restaurant!
Please prepare the following dishes:
* Chicken noodle soup
* Beef noodle soup
* Vegetable soup
Please let me know when the order is ready.
Thank you for your prompt attention to this matter.
Sincerely,
{}
""".format(username, server_address, server_address)
# 连接 SMTP 服务
smtp_client = smtplib.SMTP(server_address, server_port)
# 登录 SMTP 服务
smtp_client.login(username, password)
# 发送邮件
smtp_client.sendmail("your_name@example.com", "restaurant@example.com", message)
# 关闭 SMTP 连接
smtp_client.quit()
注意:
- 请将
your_username
和your_password
替换为您的 SMTP 服务器用户名和密码。 - 请将
server_address
和server_port
替换为您的 SMTP 服务器地址和端口。 - 请确保您的脚本具有写入 SMTP 服务的权限。