• 技术文章 >Python技术 >Python基础教程

    Python中Exchange发邮件

    小妮浅浅小妮浅浅2021-03-30 14:38:22原创3372

    1、安装exchangelib库

    pip3 install exchangelib

    2、引入模块

    exchangelib模块挺多的,其中Account, Credentials用来连接邮箱的,其他的根据你自身需求来吧,截图是所有模块:

    3、连接邮箱

    登录邮箱编码还是比较简单的:

    credentials = Credentials('域名\用户名', '密码')
     
    account = Account('邮箱', credentials=credentials, autodiscover=True)
    编写调用exchangelib库,发送邮件
    #Author Kang
     
    from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody
     
    def Email(to, subject, body):
        creds = Credentials(
            username='zhoumingkang',
            password='帐号验证的密码'
        )
        account = Account(
            primary_smtp_address='zhoumingkang@cedarhd.com',
            credentials=creds,
            autodiscover=True,
            access_type=DELEGATE
        )
        m = Message(
            account=account,
            subject=subject,
            body=HTMLBody(body),
            to_recipients = [Mailbox(email_address=to)]
        )
        m.send()
     
    cpu = 80
    mem = 70
     
    message = '''-----------运维报告------------<br>
    CPU使用率:%s<br>
    可用内存:%s<br>
    ''' %(cpu,mem)
     
    Email("zhoumingkang@cedarhd.com","主题",message)

    以上就是Python中Exchange发邮件的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

    专题推荐:python exchange
    品易云
    上一篇:Python找回文子串的方法 下一篇:Python代码中编译是什么

    相关文章推荐

    • Python中deque的操作整理• Python命令行如何运行文件• Python字典和json的比较• python代码提速有哪些方法• python如何转移数据库里的数据• python字符串如何取值• python切片步长怎样实现• python字符串格式化的方法整理• python语句和缩进的实现

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网