• Python撰写mail


    版本1   指定邮箱进行发送

    """
    说明:指定账户密码进行邮件发送
    由312051952@qq.com-->c4kaichen@163.com
    """
    
    
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    #登录邮箱步骤
    smtpConnect=smtplib.SMTP('smtp.qq.com',587)
    smtpConnect.ehlo()
    smtpConnect.starttls()
    smtpConnect.login('312051952@qq.com','邮箱密码')
    sender='312051952@qq.com'
    #receiver=[input('输入邮箱地址:')]
    receiver=['c4kaichen@163.com']  #显示收件人
    message=MIMEText('python邮件发送测试3','plain','utf-8')#邮箱内容
    message['To'] =  Header('c4kaichen@163.com')
    subject='python smtp测试3' #邮箱主题
    message['Subject']=Header(subject,'utf-8')
    smtpConnect.sendmail(sender,receiver,message.as_string())
    smtpConnect.quit()

    版本2 手动输入对方邮箱进行登录

    #发送邮件并手动输入对方邮箱
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    #登录邮箱步骤
    smtpConnect=smtplib.SMTP('smtp.qq.com',587)
    smtpConnect.ehlo()
    smtpConnect.starttls()
    smtpConnect.login('312051952@qq.com','邮箱密码')
    sender='312051952@qq.com'
    receiver=input('输入邮箱地址:')
    receiverdone=[receiver]   #要用列表显示
    #receiver=['c4kaichen@163.com']  #显示收件人
    message=MIMEText('python邮件发送测试5','plain','utf-8')#邮箱内容
    message['To'] =  Header(receiver)
    subject='python smtp测试4' #邮箱主题
    message['Subject']=Header(subject,'utf-8')
    smtpConnect.sendmail(sender,receiverdone,message.as_string())
    smtpConnect.quit()

    最新版本:

     分别输入要登录的QQ邮箱密码账号

    以及需要发送的对方账号以及邮件内容

    #发送邮件并手动输入对方邮箱
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    #登录邮箱步骤
    smtpConnect=smtplib.SMTP('smtp.qq.com',587)
    smtpConnect.ehlo()
    smtpConnect.starttls()
    user=input('请输入QQ邮箱地址:')
    passwd=input('请输入QQ邮箱密码:')
    smtpConnect.login(user,passwd)
    sender=user
    receiver=input('输入对方邮箱地址:')
    content=input('请输入要发送的内容:')
    receiverdone=[receiver]   #要用列表显示
    #receiver=['c4kaichen@163.com']  #显示收件人
    message=MIMEText(content,'plain','utf-8')#邮箱内容
    message['To'] =  Header(receiver)
    subject='python smtp测试4' #邮箱主题
    message['Subject']=Header(subject,'utf-8')
    smtpConnect.sendmail(sender,receiverdone,message.as_string())
    smtpConnect.quit()

    手动输入邮箱及密码之后手动输入对方的邮箱,邮件主题,内容进行邮件的发送工作

    # 发送邮件并手动输入对方邮箱
    # 注意此处是针对QQ邮箱进行撰写
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    # 登录邮箱步骤
    smtpConnect = smtplib.SMTP('smtp.qq.com', 587)
    smtpConnect.ehlo()
    smtpConnect.starttls()
    user = input('请输入QQ邮箱地址:')
    passwd = input('请输入QQ邮箱密码:')
    smtpConnect.login(user, passwd)
    sender = user
    receiver = input('输入对方邮箱地址:')
    subject = input('请输入邮件主题:')
    content = input('请输入要发送的内容:')
    receiverdone = [receiver]  # 要用列表显示
    # receiver=['c4kaichen@163.com']  #显示收件人
    message = MIMEText(content, 'plain', 'utf-8')  # 邮箱内容
    message['To'] = Header(receiver)
    message['Subject'] = Header(subject, 'utf-8')
    smtpConnect.sendmail(sender, receiverdone, message.as_string())
    smtpConnect.quit()

    新版本:每天的菜谱进行选择然后自动发送邮件

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    import time
    def current_time():
        time_format='%Y-%d-%m %X'
        time_current=time.strftime(time_format)
        return time_current
    #登录邮箱步骤
    def sendmail(x):
        smtpConnect=smtplib.SMTP('smtp.qq.com',587)
        smtpConnect.ehlo()
        smtpConnect.starttls()
        smtpConnect.login('312051952@qq.com','邮箱密码')
        sender='312051952@qq.com'
        receiver=['c4kaichen@163.com']  #显示收件人
        message=MIMEText(x,'plain','utf-8')#邮箱内容   此处建议采用plain  如果换成text发送过去的会是附件形式
        message['To'] =  Header('c4kaichen@163.com')
        time_current=current_time()
        subject='%s每日菜单'%time_current #邮箱主题:每日菜单
        message['Subject']=Header(subject,'utf-8')
        smtpConnect.sendmail(sender,receiver,message.as_string())
        smtpConnect.quit()
    import random
    list_menu=['排骨冬瓜','清炒包菜',' 蒜泥西蓝花',' 清炒青菜',
                         ' 筒骨汤',' 香干肉丝',' 丝瓜炒蛋',' 面条',' 沙县',' 品客']
    def write_menu(args):
        with open('menu_info','w',encoding='utf-8') as f:
            f.writelines(args)
    def read_menu():
        with open('menu_info','r',encoding='utf-8') as f2:
            data=f2.read()
        return data
    
    num=0
    item_menu=[]
    count=input('请输入今天要吃几个菜:>>').strip()
    if count.isdigit():
        count=int(count)
        while num<count:
            item=random.choice(list_menu)
            item_menu.append(item)
            num+=1
            if num==count:
                print('33[31;1m今日选择的菜单如下:33[0m')
                for p_item in item_menu:
                    print(p_item)
                write_menu(item_menu)
                x=read_menu()
                sendmail(x)
    else:
        print('请重新输入')
    input('press Enter to exit')
    View Code

    #add at 18-11-11

    import smtplib
    from email.header import Header
    from email.mime.text import MIMEText
    # 第三方 SMTP 服务
    mail_host = "smtp.163.com" # SMTP服务器
    mail_user = "c4kaichen"   # 用户名
    mail_pass = "XXXXX"  # 授权密码,非登录密码
    sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败)
    receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
    title = '标题info'#标题
    content = '正文info'  # 正文
    def sendEmail():
        message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
        message['From'] = "{}".format(sender)
        #message['To'] = ",".join(receivers)
        message['To'] = receivers
        message['Subject'] = title
        try:
            smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
            smtpObj.login(mail_user, mail_pass)  # 登录验证
            smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
            print("mail has been send successfully.")
        except smtplib.SMTPException as e:
            print(e)
    
    def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):
        email_client = smtplib.SMTP(SMTP_host)
        email_client.login(from_account, from_passwd)
        # create msg
        msg = MIMEText(content, 'plain', 'utf-8')
        msg['Subject'] = Header(subject, 'utf-8')  # subject
        msg['From'] = from_account
        msg['To'] = to_account
        email_client.sendmail(from_account, to_account, msg.as_string())
        email_client.quit()
    if __name__ == '__main__':
        sendEmail()
        # receiver = '***'
        # send_email2(mail_host, mail_user, mail_pass, receiver, title, content)

     ### add at 18-11-11    通过163邮箱每日IP推送

    import socket
    
    
    # 获取本机计算机名称
    hostname = socket.gethostname()
    #print(hostname)
    # 获取本机ip列表
    #ip = socket.gethostbyname_ex(hostname)
    ip = socket.gethostbyname_ex(hostname)
    addr=ip[2]
    
    
    #获取当前时间
    import time
    current_time=time.ctime()
    
    #获取计算机名
    pc_name=socket.gethostname()
    ip_info=current_time+' '+pc_name+' '+str(addr)
    
    
    
    import smtplib
    from email.header import Header
    from email.mime.text import MIMEText
    # 第三方 SMTP 服务
    mail_host = "smtp.163.com" # SMTP服务器
    mail_user = "c4kaichen"   # 用户名
    mail_pass = "XXXXX"  # 授权密码,非登录密码
    sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败)
    receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
    title = '每日ip'  # 标题
    content = ip_info#正文
    def sendEmail():
        message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
        message['From'] = "{}".format(sender)
        #message['To'] = ",".join(receivers)
        message['To'] = receivers
        message['Subject'] = title
        try:
            smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
            smtpObj.login(mail_user, mail_pass)  # 登录验证
            smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
            print("mail has been send successfully.")
        except smtplib.SMTPException as e:
            print(e)
    
    def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):
        email_client = smtplib.SMTP(SMTP_host)
        email_client.login(from_account, from_passwd)
        # create msg
        msg = MIMEText(content, 'plain', 'utf-8')
        msg['Subject'] = Header(subject, 'utf-8')  # subject
        msg['From'] = from_account
        msg['To'] = to_account
        email_client.sendmail(from_account, to_account, msg.as_string())
        email_client.quit()
    if __name__ == '__main__':
        sendEmail()
        # receiver = '***'
        # send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
    View Code

    升级版  邮件轰炸

    import socket
    from threading import Timer
    
    # 获取本机计算机名称
    hostname = socket.gethostname()
    #print(hostname)
    # 获取本机ip列表
    #ip = socket.gethostbyname_ex(hostname)
    ip = socket.gethostbyname_ex(hostname)
    addr=ip[2]
    
    
    #获取当前时间
    import time
    current_time=time.ctime()
    
    #获取计算机名
    pc_name=socket.gethostname()
    ip_info=current_time+' '+pc_name+' '+str(addr)
    
    
    
    import smtplib
    from email.header import Header
    from email.mime.text import MIMEText
    # 第三方 SMTP 服务
    mail_host = "smtp.163.com" # SMTP服务器
    mail_user = "c4kaichen"   # 用户名
    mail_pass = "XXXXX"  # 授权密码,非登录密码
    sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败)
    receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
    title = '每日ip'  # 标题
    content = ip_info#正文
    def sendEmail():
        message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
        message['From'] = "{}".format(sender)
        #message['To'] = ",".join(receivers)
        message['To'] = receivers
        message['Subject'] = title
        try:
            smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
            smtpObj.login(mail_user, mail_pass)  # 登录验证
            smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
            t=Timer(10,sendEmail)
            t.start()
            print("mail has been send successfully.")
        except smtplib.SMTPException as e:
            print(e)
    
    def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):
        email_client = smtplib.SMTP(SMTP_host)
        email_client.login(from_account, from_passwd)
        # create msg
        msg = MIMEText(content, 'plain', 'utf-8')
        msg['Subject'] = Header(subject, 'utf-8')  # subject
        msg['From'] = from_account
        msg['To'] = to_account
        email_client.sendmail(from_account, to_account, msg.as_string())
        email_client.quit()
    if __name__ == '__main__':
        sendEmail()
        # receiver = '***'
        # send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
  • 相关阅读:
    实反对称矩阵正则化
    小矩阵相乘效率对比:lapack, cblas, 手写函数
    python实现: VMC做一维谐振子
    一个简单矩阵的本征值问题
    python画能级图
    广义相对论笔记
    PVPC kb3g pn/upn Ti44 LAP 脚本
    自组织临界现象:沙堆模型
    c#备份MySQL数据库 转载 from
    vs2010 新特性 from
  • 原文地址:https://www.cnblogs.com/nodchen/p/8458140.html
Copyright © 2020-2023  润新知