Python self-study-class14(up)-class design and use (take email as an example)

Call the smtplib library to realize the function of sending emails, which can be group sending and email bombing;

#coding=gbk
import smtplib   #发邮件
from email.mime.text import MIMEText  #邮件文本

class SendMail:
    def __init__(self,SMTPsever,Sender,password):
        self.SMTPsever = SMTPsever  # 服务器
        self.Sender = Sender  # 发送邮件的地址
        self.password = password  # 密码
        self.mailsever = smtplib.SMTP(self.SMTPsever, 25)  # 邮件服务器端口
        self.mailsever.login(self.Sender, self.password)  # 登录
    def Send(self,Message,title,maillist):
        Message = Message  # 邮件内容
        msg = MIMEText(Message)  # 转化邮件文本

        msg["Subject"] = title  # 标题
        msg["From"] = self.Sender  # 发
        msg["To"] = "[email protected]"  # 收


        self.mailsever.sendmail(self.Sender, maillist, msg.as_string())
    def exit(self):
            self.mailsever.quit()

sender1 = SendMail("smtp.qq.com","[email protected]","henlcsjqpsedbeje") #qq的smtp服务器端口,邮箱,以及密码(该密码为smtp提供的随机码,获得方式见后文)
for i in range(10):
    sender1.Send("hello,I love python","java",["[email protected]"])#内容,标题,标题,收件人,收件人可填多个
sender1.exit()

**Operation effect: **To send 10 emails to the designated mailbox, the content is "hello, I love python", the title is "java" to
get the smtp random code:
web version qq mailbox settings, open the account section, open the following services , Generate the authorization code in the yellow box to get the login random code;
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/113247755