发普通邮件

import smtplib
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

host_server="smtp.sina.com"
sender_sina="fuxingyu_123@sina.com"
pwd="xxxxx"
sender_sina_mail="fuxingyu_123@sina.com"#发件人邮箱
receiver="280568238@qq.com"

mail_title="基金"
mali_content="哈哈哈哈"

msg=MIMEMultipart()#邮件主体
msg["Subject"]=Header(mail_title,'utf-8')
msg["From"]=sender_sina_mail
msg["To"]=Header("测试邮件","utf-8")
msg.attach(MIMEText(mali_content,"plain","utf-8"))#邮件正文

smtp=SMTP_SSL(host_server)#ssl登入
smtp.login(sender_sina,pwd)
smtp.sendmail(sender_sina_mail,receiver,msg.as_string())
smtp.quit()

发添加附件的邮件

import smtplib
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication#添加附件
from email.header import Header

host_server="smtp.sina.com"
sender_sina="fuxingyu_123@sina.com"
pwd="xxxxxx"
sender_sina_mail="fuxingyu_123@sina.com"#发件人邮箱
receiver="280568238@qq.com"

mail_title="基金"
mali_content="今天高兴的一天"
attchment=MIMEApplication(open("C://Users//fuxingyu//Desktop//食品.xlsx","rb").read())
attchment.add_header("Content-Disposition","attachment",filename="食品.xlsx")

msg=MIMEMultipart()#邮件主体
msg["Subject"]=Header(mail_title,'utf-8')
msg["From"]=sender_sina_mail
msg["To"]=Header("测试邮件","utf-8")
msg.attach(MIMEText(mali_content,"plain","utf-8"))#邮件正文
msg.attach(attchment)#添加附件

叫我瞄大人
467 声望81 粉丝

喜欢追星的非科班的编程爱好者