Python 发送邮件

# -*- coding: utf8  -*-
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib
import matplotlib.pyplot as plt

if __name__ == '__main__':
    # 以html格式构建邮件内容
    send_str = '<html><body>'
    send_str += '<center>打钱</center>'

    # html中以<img>标签添加图片,align和width可以调整对齐方式及图片的宽度
    send_str += '<img src="cid:image1" alt="image1" align="center" width=100% >'
    send_str += '<center>打钱</center>'
    send_str += '</body></html>'

    # 画图并保存到本地
    '''    pic_path = 'test.png'
        plt.plot([1, 3, 2, 4], '--r')
        plt.title('ooooo')
        plt.savefig(pic_path)
    构建message
    '''
    msg = MIMEMultipart()
    '''
   

    # 添加邮件内容
    content = MIMEText(send_str, _subtype='html', _charset='utf8')
    msg.attach(content)

    # 构建并添加图像对象
    img1 = MIMEImage(open('c:\\1.jpg', 'rb').read(), _subtype='octet-stream')
    img1.add_header('Content-ID', 'image1')
    msg.attach(img1)
    '''
    # 邮件主题
    msg['Subject'] = '这是一个打钱'

    # 邮件收、发件人
    user = "gong@mail.xxx.com.cn"
    me = "洪老大" # + "<gong@mail.xxx.com.cn>"
    to_list = ["gong@mail.xxx.com.cn"]
    msg['To'] = ';'.join(to_list)
    msg['From'] = me

    # 构建并添加附件对象
    # 如果不想直接在邮件中展示图片,可以以附件形式添加
    '''
    img = MIMEImage(open("c:\\1.jpg", 'rb').read(), _subtype='octet-stream')
    img.add_header('Content-Disposition', 'attachment', filename=pic_path)
    msg.attach(img)
'''
    # 密码(有些邮件服务商在三方登录时需要使用授权码而非密码,比如网易和QQ邮箱)
    passwd = "你的授权码"

    # 登录邮箱
    server = smtplib.SMTP("10.5.1.76",25)
    server.connect("10.5.1.76")
    print('Login Successful!')

    # 发送邮件
    server.sendmail(user, to_list, msg.as_string())
    print('Send Successful')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值