鱼C论坛

 找回密码
 立即注册
查看: 2340|回复: 2

想让系统自动发送邮件,但是代码老是出问题

[复制链接]
发表于 2019-11-12 17:59:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下:
def sent_mail():
    smtpserver = 'smtp.qq.com'
    user = ('695364380@qq.com')
    pwd = ('6391583456ljj')
    receivers=['695364380@qq.com']#收件人
    msg=MIMEMultipart()
    title='日报_'+ date
    msg.attach(MIMEText('<p><img src="cid:1"></p>', 'html', 'utf-8'))
    fp1 = open('D:\\2019年日报\\p1.png', 'rb')
    msgImage1 = MIMEImage(fp1.read())
    fp1.close()
    msgImage1.add_header('Content-ID', '<1>')
    msg.attach(msgImage1)
   
    #添加附件
    att=MIMEApplication(open('D:\\2019年日报\\copy'+str(date)+'.xlsx','rb').read())
    att.add_header('Content-Disposition', 'attachment', filename=Header('日报'+date+'.xlsx','utf-8').encode())
    msg.attach(att)

    #添加收件发件人信息
    msg['From'] = "{}".format(user)
    msg['To'] = ",".join(receivers)
    msg['Subject'] = title
   
    #发送邮件
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    #smtp.starttls()
    smtp.login(user,pwd)
    smtp.sendmail(user, receivers, msg.as_string())
    print('发送成功')
    smtp.close()
    time.sleep(10)  

错误提示:
Traceback (most recent call last):
  File "C:\Users\lujinjian\Desktop\软件\python3代码程序\邮件发送.py", line 63, in <module>
    sent_mail()
  File "C:\Users\lujinjian\Desktop\软件\python3代码程序\邮件发送.py", line 58, in sent_mail
    smtp.login(user,pwd)
  File "C:\Users\lujinjian\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 730, in login
    raise last_exception
  File "C:\Users\lujinjian\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "C:\Users\lujinjian\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 642, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'Error: \xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-b ... amp;&no=1001256)

麻烦各位老大帮帮忙
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-11-12 19:23:50 | 显示全部楼层
不是要授权码吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-13 07:05:25 | 显示全部楼层
  1. import smtplib
  2. from os.path import basename
  3. from email.mime.application import MIMEApplication
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.utils import COMMASPACE, formatdate


  7. def send_mail(send_from, send_to, subject, text, files=None,
  8.               server="127.0.0.1"):
  9.     assert isinstance(send_to, list)

  10.     msg = MIMEMultipart()
  11.     msg['From'] = send_from
  12.     msg['To'] = COMMASPACE.join(send_to)
  13.     msg['Date'] = formatdate(localtime=True)
  14.     msg['Subject'] = subject

  15.     msg.attach(MIMEText(text))

  16.     for f in files or []:
  17.         with open(f, "rb") as fil:
  18.             part = MIMEApplication(
  19.                 fil.read(),
  20.                 Name=basename(f)
  21.             )
  22.         # After the file is closed
  23.         part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
  24.         msg.attach(part)


  25.     smtp = smtplib.SMTP(server)
  26.     smtp.sendmail(send_from, send_to, msg.as_string())
  27.     smtp.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-20 17:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表