python直联(适合于企业内部的内网邮箱)发送邮件(带附件)、发送给多人、抄送给多人的示例

# coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

# 发送纯文本格式的邮件
message = MIMEMultipart('related')
message.attach(MIMEText('there is some error or warning in your project', 'plain', 'utf-8'))
att1 = MIMEText(open('lintResult.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="aaaa"'
message.attach(att1)
#发送邮箱地址
sender = 'bbbb@cccc.com'
#登陆密码
password = 'eeee'
#收件箱地址
receiver = 'eeee@cccc.com, dddd@cccc.com'
#抄送
acc = 'eeee@cccc.com, dddd@cccc.com‘

#smtp服务器
smtp_server = 'ffff'
#发送邮箱地址
message['From'] = sender
#收件箱地址
message['To'] = receiver
message['Cc'] = acc
#主题
message['Subject'] = 'this is test email for gggg project'
server = smtplib.SMTP(smtp_server,25)

server.login(sender,password)
server.sendmail(sender,receiver,message.as_string())
server.quit()
 

很多企业内网或者外网邮箱都是通过邮箱服务器来实现收发邮件并不提供ssh接口,因此需要直联25端口来发送邮件,其中aaaa,bbbb,cccc等需要读者自行替换为所需的东东,其中有些换行或者空格是我后来加入的,可能会出错,需要读者重新替换下

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值