如何使用Python发送邮件?
Python是一种高级编程语言,它具有简单、易学、易读等特点,被广泛应用于各种领域。而在邮件发送方面,Python也有着很好的表现。在Python中,我们可以使用smtplib和email模块来实现邮件发送。下面,我们将介绍如何使用Python发送qq邮件和html邮件。
python发送qq邮件
使用Python发送qq邮件,需要先开启qq邮箱的SMTP服务,并获取授权码。然后,我们可以使用smtplib和email模块来实现邮件发送。下面是一个简单的Python代码示例:
import smtplib from email.mime.text import MIMEText
mail_host = ‘smtp.qq.com’ mail_user = ‘your_email@qq.com’ mail_pass = ‘your_email_password’
sender = ‘your_email@qq.com’ receivers = [‘receiver_email@qq.com’]
message = MIMEText(‘This is a test email sent by Python.’) message[‘From’] = sender message[‘To’] = receivers[0] message[‘Subject’] = ‘Python Test Email’
smtpObj = smtplib.SMTP_SSL(mail_host, 465) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit()
python发送html邮件
使用Python发送html邮件,需要先创建一个MIMEText对象,并设置邮件内容的类型为html。然后,我们可以使用smtplib和email模块来实现邮件发送。下面是一个简单的Python代码示例:
import smtplib from email.mime.text import MIMEText
mail_host = ‘smtp.qq.com’ mail_user = ‘your_email@qq.com’ mail_pass = ‘your_email_password’
sender = ‘your_email@qq.com’ receivers = [‘receiver_email@qq.com’]
message = MIMEText('<h1>This is a test email sent by Python.</h1>’, ‘html’) message[‘From’] = sender message[‘To’] = receivers[0] message[‘Subject’] = ‘Python Test Email’
smtpObj = smtplib.SMTP_SSL(mail_host, 465) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit()
总结
Python是一种高级编程语言,它可以帮助我们实现邮件发送。使用Python发送qq邮件和html邮件,可以使用smtplib和email模块来实现。如果您正在寻找一种简单、易学、易读的邮件发送方式,不妨考虑使用Python。