• python :简单邮件发送


    PS: util文件夹中要自行创建__init__.py文件,内容可以为空

    #\util\MailSenderHelp.py
    import smtplib
    from email.mime.text import MIMEText
    import base64
    
    class Mailsender():
        def __init__(self):
            print "I am sending the mails..."
            
        def setSmtpServer(self, smtpServer):
            self.smtpserver = smtpServer
            
        def setSender(self, sender, password):
            self.sender = sender
            self.password = password
            
        def setReceiver(self,receiver):       
            self.receiver = receiver
        
        def setSubject(self, subject):
            self.subject = subject
        
        def setContent(self,content):
            self.content = content
            
        def sendMail(self):
            smtp = smtplib.SMTP()
            smtp.connect(self.smtpserver, 25)
            smtp.login(self.sender, self.password)
            
    #        msg = MIMEText(self.content)
    #        msg['From'] = self.sender
    #        msg['To'] = ",".join(self.receiver)
    #        msg['Subject'] = self.subject
            
            self.content = base64.b64encode(self.content)
            msg = "From:%s\nTo:%s\nSubject:%s\nContent-Type:text/html\nContent-Transfer-Encoding:base64\n\n%s"%(self.sender, self.receiver, self.subject,self.content)
            
    #        smtp.sendmail(self.sender, self.receiver, msg.as_string())
            smtp.sendmail(self.sender, self.receiver, msg)
            smtp.close()
            
        def __del__(self):
            print "Finish sending mails !"
    

      



    #mail.py
    
    from util.MailSenderHelp import Mailsender
    
    if __name__ == "__main__":
        receiverList = ["XXX@qq.com","XXX@qq.com","XXX@yahoo.com.cn"]
        
        mail = Mailsender()
        
        mail.setSmtpServer("smtp.126.com")
        mail.setSender("XXX@126.com", "XXXXXX")
        mail.setReceiver(receiverList)
        mail.setSubject("This is a test mail")
        mail.setContent("I have no content,do you know")
        
        mail.sendMail()
    

      

  • 相关阅读:
    idea配置tomcat
    idea中配置tomcat乱码问题--记录处理经验
    svn提交报错值 remains tree in conflict
    Vue之MVVM
    python 时间日期处理
    SVN使用指南
    linux查看硬件信息
    软件集成过程标准化的建议
    术语辨析
    科学计数e+转成正常str
  • 原文地址:https://www.cnblogs.com/cstudio/p/2708108.html
Copyright © 2020-2023  润新知