python 群发邮件
时间: 2023-10-09 21:09:28 浏览: 78
要使用Python进行群发邮件,你可以使用smtplib模块来实现。首先,你需要导入所需的模块和库,如email.mime.text、email.header和smtplib。然后,你需要设置发件人的邮箱地址、密码,以及收件人的邮箱地址列表。接下来,你可以编写邮件的正文内容,并创建一个MIMEText对象。然后,设置邮件的发件人、收件人和主题。最后,通过SMTP服务器连接并登录你的邮箱,使用sendmail方法将邮件发送给收件人,最后断开与SMTP服务器的连接。
以下是一个示例代码:
from email.mime.text import MIMEText
from email.header import Header
import smtplib
# 设置发件人信息
from_addr = 'your_email@example.com'
password = 'your_password'
# 设置收件人信息
to_addrs = ['recipient1@example.com', 'recipient2@example.com']
# 设置邮件正文内容
content = '邮件正文内容'
# 创建MIMEText对象
msg = MIMEText(content, 'plain', 'utf-8')
# 设置邮件的发件人、收件人和主题
msg['From'] = Header(from_addr)
msg['To'] = Header(",".join(to_addrs))
msg['Subject'] = Header('邮件主题')
# 连接SMTP服务器并发送邮件
server = smtplib.SMTP('smtp.qq.com')
server.connect('smtp.qq.com', 587)
server.login(from_addr, password)
server.sendmail(from_addr, to_addrs, msg.as_string())
server.quit()
请确保将上述代码中的"your_email@example.com"和"your_password"替换为你的真实邮箱地址和密码,"recipient1@example.com"和"recipient2@example.com"替换为你要发送邮件的收件人邮箱地址。这样,你就可以使用Python进行群发邮件了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python群发邮件](https://blog.csdn.net/wow0524/article/details/122339399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)