如何用python自动发送邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header
from email.utils import parseaddr,formataddr
from email.mime.application import MIMEApplication
fromaddr = 'from_user@email.com'
password = '$(^*%N6Fjn'
toaddrs = ['to_user1@email.com', 'to_user2@email.com']
body= 'Dear All, This is an auto email'
message=MIMEMultipart()
message.attach(MIMEText(body, 'plain', 'utf-8'))
message['Subject'] = 'A test email'
message['From'] = fromaddr
message['To'] = toaddrs[0]
xlsxpart = MIMEApplication(open('D:/user/file1.xlsx', 'rb').read())
xlsxpart.add_header("Content-Disposition", "attachment", filename="file1.xlsx")
message.attach(xlsxpart)
sendimagefile=open(r'D:/user/test.jpg','rb').read()
image = MIMEImage(sendimagefile)
image.add_header('Content-ID','<image1>')
image["Content-Disposition"] = 'attachment; filename="test.jpg"'
message.attach(image)
try:
server = smtplib.SMTP('smtp.exmail.qq.com')
server.set_debuglevel(1)
server.login(fromaddr,password)
server.sendmail(fromaddr, toaddrs, message.as_string())
print('success')
server.quit()
except smtplib.SMTPException as e:
print('error', e)