1、先根据自己的需求完成邮件发送python任务测试importdocxfromsmtplibimportSMTPSSLfromemail。mime。textimportMIMETextfromemail。mime。multipartimportMIMEMultipartfromemail。headerimportHeaderfromemail。mime。applicationimportMIMEApplicationfromapscheduler。schedulers。blockingimportBlockingSchedulerfromloguruimportloggerimportyaml添加日志记录logger。add(info。log,format{time:YYYYMMDDatHH:mm:ss}{level}{message},levelINFO,rotation500MB,encodingutf8)logger。add(error。log,format{time:YYYYMMDDatHH:mm:ss}{level}{message},levelERROR,rotation500MB,encodingutf8)defsztask():withopen(r配置yaml文件路径,encodingutf8)asemailFile:confyaml。load(emailFile)邮箱stmp域名hostserverconf〔hostserver〕发送者邮箱sendermailconf〔sendermail〕授权码pwdconf〔pwd〕接收者邮箱列表receiversmailconf〔receiversmail〕抄送者邮箱列表ccmailconf〔ccmail〕filedocx。Document(r附件文件路径)title邮件标题mailcontent邮件正文拼接msgMIMEMultipart(mixed)msg〔Subject〕Header(title,utf8)msg〔From〕sendermailmsg〔To〕;。join(receiversmail)msg〔Cc〕;。join(ccmail)bodyMIMEText(mailcontent,plain)msg。attach(body)withopen(r附件文件路径,rb)asf:attachmentMIMEApplication(f。read())attachment。addheader(ContentDisposition,attachment,filename附件文件名)msg。attach(attachment)try:smtpSMTPSSL(hostserver,465)smtp。setdebuglevel(1)smtp。login(sendermail,pwd)smtp。sendmail(sendermail,receiversmailccmail,msg。asstring())logger。info(发送成功)except:logger。error(发送失败)定时任务defshedutask():shedulerBlockingScheduler()sheduler。addjob(sztask,cron,hourXX,minuteXX)try:sheduler。start()except(KeyboardInterrupt,SystemExit):passifnamemain:shedutask()2、使用pyinstaller工具将py文件打包成exe文件pipinstallpyinstaller 然后打包exe文件pyinstallerFXX。py3、使用sc工具写成win服务,后台启动 打开powershell,输入以下sc。execreate服务名自定义binPathEXE文件路径netstart服务名自定义启动服务或者在任务管理起启用sc。exedelete服务名自定义删除后台服务(选)