本帖最后由 ypcgamelife 于 2019-11-4 08:58 编辑
利用exchangelib库,连接exchange server,发送邮件,如果你单位是使用exchange服务器,可以试试。将收件人,附件等信息全放在exchange群发邮件中, 最后发送情况放 结果.txt 中。
[Python] 纯文本查看 复制代码 from exchangelib import Credentials, Account
from exchangelib import Account, CalendarItem, Message, Mailbox, FileAttachment, HTMLBody
from exchangelib.items import SEND_ONLY_TO_ALL, SEND_ONLY_TO_CHANGED
import mymessagebox
import xlrd
from datetime import date,datetime
import os
import os.path
#从excel中取邮件地址、附件等信息
mypath=os.getcwd()
refile = "结果.txt"
wrefile=open(refile,'w')
file = 'exchange群发邮件.xls'
wb = xlrd.open_workbook(filename=file)#打开文件
#print(wb.sheet_names())#获取所有表格名字
sheet2 = wb.sheet_by_index(1)#通过索引获取表格
smtpuser=sheet2.cell(1,0).value
smtppass=sheet2.cell(1,1).value
credentials = Credentials(smtpuser, smtppass)
try:
account = Account(smtpuser, credentials=credentials, autodiscover=True)
except Exception as e:
mymessagebox.mymessage('连接服务器失败,请检查用户密码等信息。系统反馈:'+str(e))
wrefile.write("连接服务器失败,原因:" + str(e) + "\n")
wrefile.close()
quit()
#获取收件人,发送邮件
sheet1 = wb.sheet_by_index(0)#通过索引获取表格
mailallrow=sheet1.nrows
#print('mailallrows=',mailallrow)
for mailrow in range(1,mailallrow):
mail_recever=sheet1.cell(mailrow,0).value
#print(sender)
if mail_recever<'0':
wrefile.write("没有收件人\n")
continue
mail_cc_recever=sheet1.cell(mailrow,1).value
#print(recever)0
mail_subject=sheet1.cell(mailrow,2).value
#print(subject)
mail_contect=sheet1.cell(mailrow,3).value
#print(mail_contect)
#构造附件
mail_attfile1=sheet1.cell(mailrow,4).value
mail_attfile2=sheet1.cell(mailrow,5).value
mail_attfile3=sheet1.cell(mailrow, 6).value
if mail_cc_recever>'0':
m = Message(
account=account,
subject=mail_subject,
body=mail_contect,
to_recipients=[
Mailbox(email_address=mail_recever),
],
cc_recipients=[mail_cc_recever], # 抄送Simple strings work, too
# bcc_recipients=[
# Mailbox(email_address='xxx@sinopec.com'),
# 'xxx@sinopec.com',
# ], # 密送Or a mix of both
)
else:
m = Message(
account=account,
subject=mail_subject,
body=mail_contect,
to_recipients=[
mail_recever
]
)
if mail_attfile1>'0':
#print(mail_attfile1.split('/')[len(mail_attfile1.split('/'))-1])取文件名作为附件显示名称
myfile = FileAttachment(name=mail_attfile1.split('/')[len(mail_attfile1.split('/'))-1], content=open(mail_attfile1,'rb').read())
m.attach(myfile)
if mail_attfile2>'0':
myfile = FileAttachment(name=mail_attfile2.split('/')[len(mail_attfile2.split('/'))-1], content=open(mail_attfile2,'rb').read())
m.attach(myfile)
if mail_attfile3>'0':
myfile = FileAttachment(name=mail_attfile3.split('/')[len(mail_attfile3.split('/'))-1], content=open(mail_attfile3,'rb').read())
m.attach(myfile)
try:
m.send_and_save()
wrefile.write(mail_recever+"发送成功.\n")
except Exception as e:
wrefile.write(mail_recever+"发送失败,原因"+str(e)+"\n")
continue
if not wrefile.closed:
wrefile.close()
mymessagebox.mymessage('共发送邮件'+str(mailrow)+'封.请检查--结果.txt,检查是否每个邮件都发送成功。')
由于比较简单,就不仔细解释了,共同学习提高。
mymessagebox是自己写的一个信息提示函数。使用的是 tk的信息提示函数。主要内容如下:
[Python] 纯文本查看 复制代码 import tkinter
from tkinter import messagebox
def mymessage(mess):
root=tkinter.Tk()
root.attributes("-topmost",True)
root.withdraw() #隐藏窗口
messagebox.showinfo("提示信息",mess)
def mymessageok(mess):
root=tkinter.Tk()
root.attributes("-topmost",True)
root.withdraw() #隐藏窗口
return messagebox.askyesno("提示信息",mess)
希望不会被删帖。 |