zoukankan      html  css  js  c++  java
  • python发送邮件

     1.使用yagmail

    import yagmail
    
    
    #链接邮箱服务器
    yag = yagmail.SMTP( user="user@126.com", password="1234", host='smtp.126.com')
    
    # 邮箱正文
    contents = ['This is the body, and here is just text http://somedomain/image.png',
                'You can find an audio file attached.', '/local/path/song.mp3']
    
    # 发送邮件
    yag.send('taaa@126.com', 'subject', contents)
    # 给多个用户发送
    yag.send(['aa@126.com','bb@qq.com','cc@gmail.com'], 'subject', contents)
    # 发送带附件的邮件
    yag.send('aaaa@126.com', '发送附件', contents, ["d://log.txt","d://baidu_img.jpg"])

    2.使用email

    from email.mime.text import MIMEText
    from email.header import Header
    import smtplib
    import getpass
    
    def send_mail(text, sender, receivers, subject, host, passwd):
        # 准备邮件
        message = MIMEText(text, 'plain', 'utf8')
        message['From'] = Header(sender, 'utf8')
        message['To'] = Header(receivers[0], 'utf8')
        message['Subject'] = Header(subject, 'utf8')
    
        # 发送邮件
        smtp = smtplib.SMTP()
        smtp.connect(host)
        smtp.login(sender, passwd)
        smtp.sendmail(sender, receivers, message.as_bytes())
    
    if __name__ == '__main__':
        sender = 'raygift@163.com'
        receivers = ['752958210@qq.com']
        subject = 'python邮件测试'
        text = '这是一封邮件测试。收到不用回复
    '
        host = 'smtp.136.com'
        passwd = getpass.getpass()
        send_mail(text, sender,receivers, subject, host, passwd)
  • 相关阅读:
    《SQL Server 2008从入门到精通》--20180628
    2019国赛
    [蓝桥杯2020] 湖北省省赛
    2019第十届蓝桥杯B组决赛题解第五题
    试题 算法提高 分解质因数
    大数相加减
    试题 算法提高 汉诺塔
    试题 算法提高 欧拉函数
    试题 算法提高 天天向上-dp
    算法训练 猴子吃包子-构造
  • 原文地址:https://www.cnblogs.com/ray-mmss/p/10305952.html
Copyright © 2011-2022 走看看