无附件
from smtplib import SMTP from email.header import Header from email.mime.text import MIMEText def main(): sender = ‘1355***[email protected]‘ receivers = ‘1047**[email protected]‘ message = MIMEText(‘用python发送邮件的实例代码‘,‘plain‘,‘utf-8‘) message[‘Subject‘] = Header(‘实例代码邮件‘, ‘utf-8‘) message[‘From‘]=sender message[‘To‘]=receivers smtpter = SMTP(‘smtp.163.com‘,25) smtpter.set_debuglevel(1) smtpter.login(sender,‘******‘) smtpter.sendmail(sender,receivers,message.as_string()) smtpter.quit() print(‘邮件发送完成‘) if __name__ == ‘__main__‘: main()
有附件
from smtplib import SMTP from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def main(): sender = ‘135***[email protected]‘ receivers = ‘1047***[email protected]‘ # 创建一个带附件的邮件消息对象 message = MIMEMultipart() message[‘From‘]=sender message[‘To‘]=receivers #主题如果是中文要五个字以上 message[‘Subject‘] = Header(‘本月数据文件‘, ‘utf-8‘) # 创建文本内容 text_content = MIMEText(‘附件中有本月的数据请查收‘,‘plain‘,‘utf-8‘) # 将文本内容添加到邮件消息对象中 message.attach(text_content) #打开附件内容 with open(‘helloworld.py‘) as f: #读取附件 txt = MIMEText(f.read(),‘base64‘,‘utf-8‘) #以文本附件传送 txt[‘Content-Type‘]=‘text/plain‘ txt[‘Content-Disposition‘]=‘attachment;filename=helloworld.py‘ #添加入邮件消息对象 message.attach(txt) smtpter = SMTP(‘smtp.163.com‘,25) smtpter.set_debuglevel(1) smtpter.login(sender,‘******‘) smtpter.sendmail(sender,receivers,message.as_string()) smtpter.quit() print(‘邮件发送完成‘) if __name__ == ‘__main__‘: main()
原文地址:https://www.cnblogs.com/Json-Five/p/12254683.html
时间: 2024-10-13 19:24:30