python发送邮件带附件_python发送邮件带附件

Exchange发送邮件

config.email_url :邮箱服务地址

config.email_user :账户

config.email_pas:密码

import config

import os

from exchangelib import DELEGATE, Account,Credentials,Configuration,NTLM,Message,Mailbox,HTMLBody,FileAttachment

from exchangelib.protocol import BaseProtocol,NoVerifyHTTPAdapter

BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter

cred = Credentials(config.email_user,config.email_pass)

con = Configuration(server=config.email_url,credentials=cred,auth_type=NTLM)

account = Account(primary_smtp_address=config.email_user, config=con,autodiscover=False,access_type=DELEGATE)

class Mail():

def __init__(self,to_list,sub,content,attach_list=[]):

self.to_list = to_list

self.sub = sub

self.content = content

self.attach_list = attach_list

def send_mail(self):

#mail_host = "smtp.126.com"

to_list = []

for address in self.to_list:

to_list.append(Mailbox(email_address=address))

try:

m = Message(account=account,folder=account.sent,subject=self.sub,body=self.content,to_recipients=to_list)

if self.attach_list:

for fpath in self.attach_list:

if not os.path.isfile(fpath):

continue

with open(fpath,'rb') as f:

cont = f.read()

name = os.path.basename(fpath)

attachf = FileAttachment(name=name,content=cont)

m.attach(attachf)

m.send_and_save()

return True,'success'

except Exception as e:

return False,str(e)

if __name__=='__main__':

content = '''邮件发送 测试'''

mail = Mail(['mrhusong@126.com'],"测试",content)

print(mail.send_mail())

163邮箱发送邮件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值