两款python smtp发邮件代码

例1

 代码如下 复制代码

from email.mime.text import MIMEText  
from email.mime.audio import MIMEAudio
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

#发送邮件
def sendMail():
    sender = "haiwil2012@yahoo.cn"
    receiver = "haiwil2013@yahoo.cn"
    subject = "Testing"
    body = "----测试发送邮件----n ----恭喜你成功发送了一封邮件----n ----测试发送邮件----n"
    im = "1.jpg"
    audio = "1.mp3"
   
    m = MIMEMultipart()
    m["to"] = receiver
    m["from"] = sender
    m["subject"] = subject
   
    m.attach(MIMEText(body))
    image = MIMEImage(open(im,"rb").read(),"jpg")
    m.attach(image)
    apart = MIMEAudio(open(audio,"rb").read(),"mp3")
    apart.add_header("Content-Disposition","attachment",filename=audio)
    m.attach(apart)
   
    s = smtplib.SMTP()
    s.connect("smtp.mail.yahoo.com")
    s.login('haiwil2012@yahoo.cn', 'hf5555')
    s.sendmail(sender,[receiver],m.as_string())
    s.close
    print 'send mail successfully'

支持smtp验证。代码如下:

 代码如下 复制代码

#!/usr/bin/env python
# -*- coding: gbk -*-
#导入smtplib和MIMEText
import smtplib
from email.mime.text import MIMEText
#############
#要发给谁,这里发给2个人
mailto_list=["aaa@juyimeng.com","bbb@juyimeng.com"]
#####################
#设置服务器,用户名、口令以及邮箱的后缀
mail_host="smtp.126.com"
mail_user="xxx"
mail_pass="yyy"
mail_postfix="126.com"
######################
def send_mail(to_list,sub,content):
    '''
    to_list:发给谁
    sub:主题
    content:内容
    send_mail("aaa@126.com","sub","content")
    '''
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content)
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user,mail_pass)
        s.sendmail(me, to_list, msg.as_string())
        s.close()
        return True
    except Exception, e:
        print str(e)
        return False
if __name__ == '__main__':
    if send_mail(mailto_list,"subject","content"):
        print "发送成功"
    else:
        print "发送失败"

时间: 2024-09-20 16:54:22

两款python smtp发邮件代码的相关文章

php中通过smtp发邮件的类,测试通过_php实例

smtp.php 复制代码 代码如下: <?php class smtp {     /* Public Variables */     var $smtp_port;     var $time_out;     var $host_name;     var $log_file;     var $relay_host;     var $debug;     var $auth;     var $user;     var $pass;     /* Private Variables

两款超php分页类代码

两款超php教程分页类代码 <?php  /**  * description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格.  * 2.0增加功能:支持自定义风格,自定义样式,同时支持php4和php5,  * example:  * 模式四种分页模式:  * require_once('../libs/classes/page.class.php');  * $page=new page(array('total'=>1000,'perpage'=>20)

asp使用jmail 发邮件代码

要用jmail  组件后才能使用哦. Sub Jmail(email,topic,mailbody)  On Error Resume Next  Dim JMail  Set JMail=Server.CreateObject("JMail.Message")  JMail.silent=True  JMail.Logging=True  JMail.Charset="gb2312"  If Not(Newasp.MailUserName = "&quo

php下使用SMTP发邮件的代码_php技巧

最近一个项目需要用到SMTP发送邮件,之前的库类不存在了,又不喜欢安装pear或者使用pear的net/smtp类,感觉太复杂了.就直接从discuz中抽取出核心稍微修改了下.    从协议分析网上,查找到SMTP协议的命令和应答,SMTP协议在发送SMTP和接收SMTP之间的会话是靠发送SMTP的SMTP命令和接收SMTP反馈的应答来完成的.常用的命令如下:    HELLO<domain><CRLF>识别发送方到接收SMTP的一个HELLO命令    MAIL FROM:<

两款右下角浮动广告js代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.111cn.net/1999/xhtml"> <head> <title> float message <

世上最好的发邮件代码

Code  1 using System;    2     3 using System.Net;    4     5 using System.Net.Mail;    6     7 using System.Net.Mime;    8     9    10    11 namespace Fish 12    13 {   14    15     /// <summary>   16    17     /// 发送邮件的类   18    19     /// </su

两款不间断向上滚动代码

  <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <title>连续向下滚动</title> <style type=&

ORACLE 自动发邮件代码

    该程序脚本最主要的功能实现为通过oracle自带的过程包发送邮件来监控ETL的执行情况:    ORACLE_SID=orcl    ORACLE_BASE=/opt/oracle    ORACLE_HOME=/opt/oracle/product/10.2.0    export ORACLE_SID ORACLE_BASE ORACLE_HOME    PWD_DIR=/home/oracle/shell    SQLPLUS=${ORACLE_HOME}/bin/sqlplus 

发邮件时终于可以通过sina的smtp验证了(附代码)

sina|smtp|发邮件 import javax.mail.*;import javax.mail.internet.*;import java.util.*; public class sendMail{    public static void main(String args[]) throws Exception    {         String host = "smtp.sina.com.cn";        String from =  "javam