美文网首页
Python3 发送邮件

Python3 发送邮件

作者: 碎念枫子 | 来源:发表于2019-05-28 17:47 被阅读0次

    代码是最好的文档

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    class SendMessage:
        def __init__(self, ):
            return
    
        def send(self, to_msg, to_add):
            mail_host = "smtp.163.com"  # 设置服务器
            mail_user = "xxxx@163.com"  # 用户名
            mail_pass = "youxiangmima"  # 口令
            sender = 'xxx@163.com'
            receivers = [to_add]  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
            conten = to_msg
            message = MIMEText(conten, 'plain', 'utf-8')
            message['From'] = "xxxx@163.com"
            message['To'] = to_add
            subject = '这个是标题'
            message['Subject'] = Header(subject, 'utf-8')
            try:
                smtpObj = smtplib.SMTP()
                smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
                smtpObj.login(mail_user, mail_pass)
                smtpObj.sendmail(sender, receivers, message.as_string())
                print("邮件发送成功")
            except smtplib.SMTPException as e:
                print("Error: 无法发送邮件" + e.strerror)
    
    def main(argv):
        sender = SendMessage()
        sender.send('你好', 'xxxx@qq.com')
    
    if __name__ == '__main__':
        main(sys.argv)
    
    

    相关文章

      网友评论

          本文标题:Python3 发送邮件

          本文链接:https://www.haomeiwen.com/subject/apxjtctx.html