email抄送 python
时间: 2023-12-28 18:26:15 浏览: 106
以下是使用Python发送带有抄送的电子邮件的示例代码:
```python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 设置发件人、收件人和抄送人
sender = 'sender@example.com'
receiver = 'receiver@example.com'
cc = ['cc1@example.com', 'cc2@example.com']
# 创建邮件对象
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Cc'] = ', '.join(cc)
msg['Subject'] = 'Test Email'
# 添加正文
body = 'This is a test email.'
msg.attach(MIMEText(body, 'plain'))
# 发送邮件
smtp_server = 'smtp.example.com'
smtp_port = 587
smtp_username = 'username'
smtp_password = 'password'
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)
server.sendmail(sender, [receiver] + cc, msg.as_string())
print('Email sent successfully!')
except Exception as e:
print('Failed to send email:', str(e))
```
请注意,您需要将示例代码中的发件人、收件人、抄送人、SMTP服务器、SMTP端口、SMTP用户名和密码替换为您自己的信息。
阅读全文