写一个批量发送邮件的工具类
时间: 2024-05-09 10:19:40 浏览: 16
作为AI语言模型,我无法编写和运行实际的代码,但我可以提供一个伪代码框架,你可以根据这个框架来编写代码。
```
class EmailSender:
def __init__(self, sender_email, sender_password, smtp_server, smtp_port):
self.sender_email = sender_email
self.sender_password = sender_password
self.smtp_server = smtp_server
self.smtp_port = smtp_port
def send_email(self, recipient_email, subject, body):
# Use SMTP to send email
smtp_obj = smtplib.SMTP(self.smtp_server, self.smtp_port)
smtp_obj.ehlo()
smtp_obj.starttls()
smtp_obj.login(self.sender_email, self.sender_password)
message = f"Subject: {subject}\n\n{body}"
smtp_obj.sendmail(self.sender_email, recipient_email, message)
smtp_obj.quit()
def send_emails(self, recipient_list, subject, body):
for recipient_email in recipient_list:
self.send_email(recipient_email, subject, body)
```
这个类有两个方法:
1. `send_email` 方法用于发送单个邮件给指定的收件人。
2. `send_emails` 方法用于批量发送邮件给多个收件人,它接收一个收件人列表作为参数。
在这个类的构造函数中,你需要提供你的发件人邮箱、密码、SMTP 服务器地址和端口号。
你可以根据你的实际需求来修改这个类,例如添加附件、使用 HTML 格式的内容等。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)