• 发送邮件


    使用yagmail包

    import yagmail
    
    # 链接邮箱服务器
    yag = yagmail.SMTP(user="124xxxxx@qq.com", password="gkcooyxxxxxxx", host='smtp.qq.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('lvxxxxxx@xxxxx666.cn', 'Python email test', contents)
    
    """给多个用户发邮件:
    只需要将接收邮箱 变成一个list即可。
    """
    # 发送邮件
    # 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"])
    
    
    """抄送"""
    # 邮箱正文  文本及附件
    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', '测试邮件', 'test.html', 'logo.jpg',
                'yagmal_test.txt']
    
    # 发送邮件
    yag.send(to='xx@xx.com', cc='xxx@xxx.com', subject='发送附件', contents=contents)

    或者

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    # 发送邮箱服务器
    smtpserver = 'smtp.qq.com'
    # 发送邮箱用户
    user = '12457xxxxx@qq.com'
    # smtp授权码
    password = 'gzzzssszzxxxxxxxx'
    # 发送邮箱
    sender = '1245xxxxx@qq.com'
    # 接收邮箱
    receiver = 'lvxxxxxx@xxxxx666.cn'
    # 发送邮件主题
    subject = 'Python email test'
    
    
    # 编写HTML类型的邮件正文
    msg = MIMEText('<html><h1>你好!</h1></html>', 'html', 'utf-8')
    msg['Subject'] = Header(subject, 'utf-8')  # 邮件的主题,也可以说是标题
    msg['From'] = Header('嗯嗯嗯', 'utf-8')  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
    msg['To'] = Header('么么么哒', 'utf-8')  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
    
    # 连接发送邮件
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
  • 相关阅读:
    使用java中的注解@see
    MacOS软件默认安装路径
    学习MACD指标
    go CD 用虚拟机快速增加一个新agent
    git推送本地分支到远程分支
    git如何切换远程仓库
    git命令查看远程分支
    Java 学习札记(一)JDK安装配置
    Oracle 基本操作符
    C# 常用控件属性及方法介绍
  • 原文地址:https://www.cnblogs.com/lvchengda/p/12617925.html
Copyright © 2020-2023  润新知