[Python-14/100-B] 网络应用开发(Internet Application Development)

版权声明:公众号:Fresh Site。QQ群:690274159。转载我的博文时,请附上转载地址,谢谢!^_^我是嘻哈程序猿freshman。 https://blog.csdn.net/wuhongxia29/article/details/90797842

Day14-B 网络应用开发

发送电子邮件

SMTP(简单邮件传输协议)-TCP(传输控制协议)

运行如下代码时,先在邮箱“设置”开启SMTP服务。

# Python发送邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header


def main():
    sender = '[email protected]'
    pwd = 'password'
    receivers = ['[email protected]']

    # 参数(文本内容,文本格式,编码)
    message = MIMEText('用Python发送邮件的示例代码', 'plain', 'utf-8')
    message['From'] = Header('[email protected]', 'utf-8')
    message['To'] = Header('[email protected]', 'utf-8')
    message['Subject'] = Header('示例代码实现邮件', 'utf-8')

    subject = 'Python邮件测试'
    message['Subject'] = Header('测试', 'utf-8')

    try:
        # 使用非本地服务器,需要建立ssl连接
        smtper = smtplib.SMTP_SSL('smtp.126.com', 465)
        smtper.login(sender, pwd)
        smtper.sendmail(sender, receivers, message.as_string())
        print('邮件发送成功!')
    except smtplib.SMTPException as e:
        print('Error:无法发送邮件.Case:%s' % e)


if __name__ == '__main__':
    main()

发送短信

猜你喜欢

转载自blog.csdn.net/wuhongxia29/article/details/90797842