python smtp模块发送邮件的代码

发布时间:2020-05-13编辑:脚本学堂
本文分享下,用python的smtp模块发送邮件的一个例子,有需要的朋友可以作个参考。

python smtp邮件发送实例代码,如下:
 

复制代码 代码示例:

import smtplib, mimetypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import os

class smtpclient:
    port=25
    stmpAddr= None
    username=None
    password=None
    localMaill=None
    mailserver=None
    debugLevel=0
    def init_smtp(self):
        pass
    def relase_smtp(self):
        if self.mailserver != None :
            self.mailserver.close()
                           
    def set_debugLevel(self,_level):
        self.debugLevel = _level
    def open_smtp(self,_host,_port):
        self.host = _host
        self.port = _port
        try:
            self.mailserver=smtplib.SMTP(_host,_port)
            if self.debugLevel > 0:
                print("smtplib init Ok")
            return True
        except:
            self.mailserver=None
            if self.debugLevel > 0:
                print("smtplib init error")
            return False
    def login_mail(self,_user,_password):
        if self.mailserver != None:
            self.username = _user
            self.password = _password
            try:
                self.mailserver.login(_user, _password)
                if self.debugLevel > 0:
                    print("smtplib login Ok")
                return True
            except:
                if self.debugLevel > 0:
                    print("smtplib login error")
                return False
        else:
            return False
    def close_mail(self):
        if self.mailserver != None:
            self.mailserver.close()
            if self.debugLevel > 0:
                print("close mail Ok n")
            return True
        else:
            return False
    def send_mail(self,_from,_to,_title,_msg):
        if self.mailserver != None:
            try:
                #mail head
                msg = MIMEMultipart()
                msg['From'] = _from
                msg['To'] = _to
                msg['Subject'] = _title
                #mail msg
                txt = MIMEText(_msg)
                msg.attach(txt)
                self.mailserver.sendmail(_from, _to, msg.as_string())
                if self.debugLevel > 0:
                    print("smtplib send Ok")
                return True
            except:
                if self.debugLevel > 0:
                    print("smtplib send error")
                return False
        else:
            return False
                   
mail =smtpclient()
mail.set_debugLevel(1)
mail.open_smtp("smtp.yeah.net", 25)
mail.login_mail("xxxx", "xxxxx")
mail.send_mail("xxxxx@yeah.net", "xxxx@jb200.com", "test for python", "hello wolrdn")
mail.close_mail()

您可能感兴趣的文章:
分享:python发邮件的综合实例
python邮件发送模块smtplib的实例详解
python smtplib发送邮件的例子 python使用126邮箱发送邮件
python smtplib模块发邮件(带附件)的例子
Python发送带附件的邮件的实现代码
python 发送邮件乱码的解决方法
python从文件读取邮件地址输出的例子
python使用gmail发送邮件的实例代码
python smtplib模块发送邮件的实例详解
python发送邮件的脚本一例
python结合php解决发送邮件乱码的问题
python发送邮件的例子
python发送邮件的实例代码
python 发送邮件的代码