新闻系统的定时通知初步有三种实用方式,1.短信 2.邮箱 3.微信

短信就不得不使用第三方平台,虽说5分一条,但耐不住量大,一天1000条的话,50元也是一笔不小的支出。

这时,邮箱和微信的优势就体现出来了,而且经测试,邮箱的并发性还是挺不错的。

以下是自己实现的简单封装

import smtplib
import email.mime.multipart
import email.mime.text

class Emailer(object):
    def __init__(self,account,password):
        """初始化账号密码"""
        self.msg = email.mime.multipart.MIMEMultipart()
        self._account = account
        self._password = password

    def send_email(self,send_email,title,content):
        """发送邮件"""
        self.msg['from'] = self._account
        self.msg['to'] = send_email
        self.msg['subject'] = title
        content = content
        txt = email.mime.text.MIMEText(content)
        self.msg.attach(txt)

        smtp = smtplib.SMTP()
        smtp.connect('smtp.163.com') # 使用的发送者邮箱的那啥来着,post
        smtp.login(self._account, self._password)
        smtp.sendmail(self._account, send_email, str(self.msg))
        print("发送成功")
        smtp.quit()

if __name__ == "__main__":
    emailer = Emailer("账号","密码")
    title = "大事不好了"
    content = "....."
    emailer.send_email("接受邮箱",title,content)

  

然后就遇到了一个问题,在windows下运行没有问题,但是一旦放到服务器上则一直卡死状态,无法发送,网上百度原来发现是端口问题,修改上述代码发送成功.

import smtplib
import email.mime.multipart
import email.mime.text

class Emailer(object):
    def __init__(self,account,password):
        """初始化账号密码"""
        self.msg = email.mime.multipart.MIMEMultipart()
        self._account = account
        self._password = password

    def send_email(self,send_email,title,content):
        """发送邮件"""
        self.msg['from'] = self._account
        self.msg['to'] = send_email
        self.msg['subject'] = title
        content = content
        txt = email.mime.text.MIMEText(content)
        self.msg.attach(txt)

        smtp = smtplib.SMTP_SSL()
        smtp.connect('smtp.163.com',465) # 使用的发送者邮箱的那啥来着,post
        smtp.login(self._account, self._password)
        smtp.sendmail(self._account, send_email, str(self.msg))
        print("发送成功")
        smtp.quit()

if __name__ == "__main__":
    emailer = Emailer("账号","密码")
    title = "大事不好了"
    content = "....."
    emailer.send_email("接受邮箱",title,content)

smtp的更多相关文章

  1. c#smtp多线程

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. python smtp 群发邮件

    最近工作中遇到使用脚本处理问题并发送结果邮件,使用python的smtp模块很简单的完成了实现.今天遇到一个问题,根据脚本的测试结果需要群发邮件,但是发送邮件的py文件只有首个地址收到了邮件.仔细排查 ...

  3. SSRS1:配置SMTP Server发送mail

    为了使用SSRS发送mail,必须为Reporting service配置SMTP Server. 1,在Reporting Service Configuration Manager中配置Email ...

  4. CentOS6.5_64位系统下安装配置postfix邮件系统 启用并配置SMTP在第三方上边使用发送邮件

    一前言 本来使用qq邮箱发送邮件,然后借助sendEmail来发送邮件告警,但是有每天的200封限制,很是不爽,于是想到来自己搭建邮件服务器来解决这个问题,关于使用sendEmail来借助qq邮箱来发 ...

  5. Python SMTP邮件模块

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件, ...

  6. 本地测试SMTP服务器

    一年前看计算机网络里的电子邮件协议的时候,想自己建一个本地SMTP服务器,然后用telnet发送消息,后来是没找到.这次又翻看了一下,找了个Windows平台下的hMailServer,挺方便的.步骤 ...

  7. SMTP Error: Could not connect to SMTP host

    PHPMailer是一个非常棒的开源邮件类,使用也非常简单,但是对于虚拟主机来说,往往要受到各种限制.刚才我在虚拟主机上使用PHPMailer就遇到一个“SMTP Error: Could not c ...

  8. 使用phpmailer发送smtp邮件时提示 SMTP Error: Could not authenticate 错误

    使用phpmailer发送smtp邮件时提示 SMTP Error: Could not authenticate 错误 这个错误是验证出现错误, $mail->Port = 25; //SMT ...

  9. php用smtp发送邮件

    php用smtp发送邮件 1.其实用smtp协议发送邮件很简单,用框架或者原生都可以,我们需要用到class.phpmailer.php 和class.smtp.php,大家可以去网上下载. 这是一个 ...

  10. C#如何利用QQ邮箱SMTP发送邮件

    public void SendEmail() { MailMessage msg = new MailMessage(); msg.To.Add("to@qq.com");//收 ...

随机推荐

  1. Linux sudoers

    xxx is not in the sudoers file.This incident will be reported.的解决方法 - xiaochaoyxc - 博客园http://www.cn ...

  2. Display Hibernate SQL to console – show_sql , format_sql and use_sql_comments

    (转)灵活控制 Hibernate 的日志或 SQL 输出,以便于诊断 - CS408 - 博客园 https://www.cnblogs.com/lixuwu/p/7479496.html Disp ...

  3. [转帖]ulimit、limits.conf、sysctl和proc文件系统

    ulimit.limits.conf.sysctl和proc文件系统 来源:https://blog.csdn.net/weixin_33918114/article/details/86882372 ...

  4. 工程下CmakeLists.txt

    2.工程下Cmake 本小节的任务是让上一小结的程序更像一个工程: 为工程添加一个子目录 src,用来放置工程源代码 : 添加一个子目录doc,用来放置这个工程的文档 hello.txt: 在工程目录 ...

  5. AngularJS:directive自定义的指令

    除了 AngularJS 内置的指令外,我们还可以创建自定义指令. 你可以使用 .directive 函数来添加自定义的指令. 要调用自定义指令,HTML 元素上需要添加自定义指令名. 使用驼峰法来命 ...

  6. C# Note14: Editable WPF ListView

    (1)https://stackoverflow.com/questions/5652527/editable-wpf-listview (2)How to: Create a ListView wi ...

  7. Object.prototype.toString.call()

    源码中有这样一段: class2type = {}, toString = class2type.toString,   function type(obj) { //obj为null或者undefi ...

  8. Golang的interface实践

    这是第二个我在别的语言里面没有见过的实现,go的interface可以说是独树一帜,让我们仔细来实践一下. interface类型是什么?interface类型定义了一组方法,如果某个对象实现了某个接 ...

  9. DNS_PROBE_FINISHED_NXDOMAIN & MacOS

    DNS_PROBE_FINISHED_NXDOMAIN 内网 DNS bug 8.8.8.8 8.8.4.4 # new inner Wi-Fi 10.1.3.10 10.1.3.13 Windows ...

  10. npm安裝、卸載、刪除、撤銷發佈包、更新版本信息

    利用npm安裝包: 全局安裝:npm install -g 模塊安裝 局部安裝(可以使用repuire(‘模塊名’)引用):npm install 模塊名稱 如果權限不夠,就是用管理員方式安裝. 本地 ...