import smtplib
import traceback
from email.mime.text import MIMEText
from config.config import *                                     #config.config为配置文件,文件内容有smtp_host ,smtp_from_email ,smtp_pwd ,smtp_port 几个参数

class EmailSender(object):
  def __init__(self):
    self.smtp_host=smtp_host                                     #发送邮件的smtp服务器(QQ邮箱:"smtp.qq.com",163邮箱:"smtp.163.com")
    self.smtp_from_email=smtp_from_email               #邮件发送者的邮箱
    self.smtp_pwd=smtp_pwd                                     #邮件发送者的邮箱的授权码
    self.smtp_port=smtp_port                                      #smtp邮箱的端口,默认是465

  def send_email(self,to_email_list,subject,body):
    '''
    发送邮件
    param to_email_list: 收件人邮箱列表,格式["123@qq.com","123@163.com"]
    param subject: 邮件主题,格式:"邮件主题"
    param body: 邮件内容, 格式:"邮件所说的内容"
    '''
    msg=MIMEText(body,'plain','utf-8')        #把内容转换成指定格式;邮箱内容,格式,编码
    msg["From"]=self.smtp_from_email       #发件人
    msg["To"]=",".join(to_email_list)          #收件人列表,转换成string类型,使用逗号隔开
    msg["Subject"]=subject              #邮件标题
    try:
      SmtpSslClient=smtplib.SMTP_SSL(self.smtp_host,self.smtp_port)        #实例化一个SMTP_SSL对象
      Loginer=SmtpSslClient.login(self.smtp_from_email,self.smtp_pwd)        #登录smtp服务器
      print("登录结果:Loginer=",Loginer) #loginRes = (235, b'Authentication successful')
      if Loginer[0]==235:
        print("登录成功,code=",Loginer[0])
        SmtpSslClient.sendmail(self.smtp_from_email,to_email_list,msg.as_string())      #发件人,收件人列表,邮件内容
        print("mail has been send successfully,message:",msg.as_string())
        SmtpSslClient.quit()                          #退出邮箱
      else:
        print("邮件登录失败,发送失败。code=",Loginer[0],"message=",msg.as_string())
    except Exception as e:
      traceback.print_exc()        #打印堆栈报错信息
      print("邮件发送失败,报错信息:",e)

if __name__=="__main__":
  senders=EmailSender()
  senders.send_email(["123456@qq.com","123456@163.com"],"测试邮件发送的标题","这是一个测试邮件发送的内容")

参考博客:https://blog.csdn.net/qq_27648991/article/details/82898775

python 发送无附件邮件的更多相关文章

  1. python 发送带附件的邮件

    特别注意的地方:filespart.add_header("Content-Disposition","attachment",filename=file_na ...

  2. ORACLE发送带附件邮件的二三事之一

    在oracle使用过程中,我们可以通过pl/sql生成数据文件,也可以通过spool on spool off生成,但某些环境下,我们需要通过存储过程处理数据,数据处理完,需要自动生成数据文件,手工导 ...

  3. Python发送带附件的邮件

    看别人的博客就不要在往别人的邮箱里发东西了行不行, 有点素质可以吗!!! 写出来分享还不知道珍惜!!!!! #-*-encoding:utf-8 -*- import os import smtpli ...

  4. 使用python发送简单的邮件

    from:http://blog.csdn.net/zhaoweikid/article/details/125898 前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeB ...

  5. Spring发送带附件邮件

    下面是一个例子使用Spring通过Gmail SMTP服务器来发送电子邮件附件.为了包含附件的电子邮件,你必须使用 Spring的JavaMailSender及MimeMessage 来代替 Mail ...

  6. python 发送带附件的 邮件

    from email.MIMETextimportMIMETextfrom email.MIMEMultipartimportMIMEMultipartimport smtplib mail_host ...

  7. 【python】使用python发送文本内容邮件

    下面提供了一个使用python做的发送文本内容的邮件代码,能够在邮件内容中设置文字颜色,大小,换行等功能. #auther by zls #_*_coding:utf-8_*_ import sys ...

  8. 使用python发送和接收邮件

    关于电子邮件 大学之前,基本不用邮箱,所以基本感觉不到它的存在,也不知道有什么用:然而大学之后,随着认识的人越来越多,知识越来越广泛,邮箱已然成为很重要的通讯工具,大学一些课程作业需要有邮箱发给老师, ...

  9. 利用spring-mail模块发送带附件邮件dome

    本例为maven项目,直接撸代码吧. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

随机推荐

  1. iOS12

    ios 12 中的新特性. 已经有很多文章写得很好,这里不再赘述,直接给大家几个传送门: https://sspai.com/post/47168 https://post.smzdm.com/p/7 ...

  2. PHP,PSR开发规范

    https://github.com/hfcorriez/fig-standards/tree/zh_CN/%E6%8E%A5%E5%8F%97 PSR-1-basic-coding-standard ...

  3. C#中IEnumerable、ICollection、IList、List之间的区别

    IEnumerable.ICollection.IList.List之间的区别,本文分别分析了它的实现源码,从而总结出了它们之间的关系和不同之处. 首先我看看 IEnumerable: // 摘要: ...

  4. There is a chart instance already initialized on the dom!警告

    使用Echarts插件的时候,多次加载会出现There is a chart instance already initialized on the dom.这个错误,改插件已经加载完成. 并且如果你 ...

  5. Android Studio系列-签名打包

    本篇博客纪录使用Android Studio对项目进行签名打包,跟Eclipse大同小异,读者朋友注意其中到差别. 第一步 创建签名文件 第二步 填写签名参数 第三步 选择构建类型 第四步 查看生成第 ...

  6. Python实现正则表达式匹配任意的邮箱

    首先来个简单的例子,利用Python实现匹配163邮箱的代码: #-*- coding:utf-8 -*- __author__ = '杨鑫' import re text = input(" ...

  7. FastDFS常用命令

    1.启动FastDFS tracker: /usr/local/bin/fdfs_trackered %FastDFS%/tracker.conf storage: /usr/local/bin/fd ...

  8. Zookeeper —— 初识

    什么是 Zookeeper Zookeeper 是一个开放源代码的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现: Zookeeper 是典型的分布式数据一致性的解决方案,分布 ...

  9. 利用rqalpha帅选股票 1

    最近股市好了,然后过了3100点后躺着赚钱已经不容易了,股票又太多,想着现在也是做这个东西,倒不如再进一步,把之前研究的量化交易利用起来. rqalpha很早就开源了,之前看过,貌似用来选股什么的很好 ...

  10. Springboot中如何在Utils类中使用@Autowired注入bean

    Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类: 1. 使用@Component注解标记工具类Statisti ...