美文网首页
阿里云的三种链接邮箱服务器发邮件方式

阿里云的三种链接邮箱服务器发邮件方式

作者: 你飞跃俊杰 | 来源:发表于2019-02-21 12:42 被阅读7次
    image

    本人学得比较渣,python和php都会一丢丢,各位大佬不要见怪

    注意:

    1.第一种需向阿里云售后开端口,可是售后死活不肯开.....(也许人家没权限开
    2.第二、第三种,需开通阿里云邮件推送功能,目前是前200封免费
    3.第一种跟第三种的区别只在于,第一种填自己的host,第三种固定是阿里云的host(smtpdm.aliyun.com)
    4.第二种需要下载阿里云的SDK,用SDK连控制台发送出去,类似分享功能差不多,需要appID和appkey


    第一种,自己写程序,通过25端口连接发邮箱的服务器发邮箱

    # -*- coding:utf-8 -*-
    
    import urllib, urllib2
    
    import smtplib
    
    from email.mime.multipart import MIMEMultipart
    
    from email.mime.text import MIMEText
    
    from email.mime.application import MIMEApplication
    
    from email.header import Header
    
    username = '发件人邮箱'
    
    password = '发件人密码'
    
    # 收件人地址列表,支持多个收件人,最多30个
    
    rcptlist = ['收件人邮箱']
    
    receivers = ','.join(rcptlist)
    
    # 构建 multipart 的邮件消息
    
    msg = MIMEMultipart('mixed')
    
    msg['Subject'] = 'Test Email'
    
    msg['From'] = username
    
    msg['To'] = receivers
    
    # 构建 multipart/alternative 的 text/plain 部分
    
    alternative = MIMEMultipart('alternative')
    
    textplain = MIMEText('纯文本部分', _subtype='plain', _charset='UTF-8')
    
    alternative.attach(textplain)
    
    # 构建 multipart/alternative 的 text/html 部分
    
    texthtml = MIMEText('超文本部分', _subtype='html', _charset='UTF-8')
    
    alternative.attach(texthtml)
    
    # 将 alternative 加入 mixed 的内部
    
    msg.attach(alternative)
    
    # 附件类型
    
    # # xlsx 类型的附件
    
    # xlsxpart = MIMEApplication(open('xlsx.xlsx', 'rb').read())
    
    # File file = new
    
    # xlsxpart.add_header('Content-Disposition', 'attachment', filename=Header("xlsx.xlsx","utf-8").encode())
    
    # msg.attach(xlsxpart)
    
    # # jpg 类型的附件
    
    jpgpart = MIMEApplication(open('jpg.jpg', 'rb').read())
    
    jpgpart.add_header('Content-Disposition', 'attachment', filename=Header("jpg.jpg","utf-8").encode())
    
    msg.attach(jpgpart)
    
    # # mp3 类型的附件
    
    # mp3part = MIMEApplication(open('mp3.mp3', 'rb').read())
    
    # mp3part.add_header('Content-Disposition', 'attachment', filename=header("mp3.mp3","utf-8").encode())
    
    # msg.attach(mp3part)
    
    # pdf 类型的附件
    
    pdfpart = MIMEApplication(open('pdf.pdf', 'rb').read())
    
    pdfpart.add_header('Content-Disposition', 'attachment', filename='pdf.pdf')
    
    msg.attach(pdfpart)
    
    # 发送邮件
    
    try:
    
        client = smtplib.SMTP()
    
        #python 2.7以上版本,若需要使用SSL,可以这样创建client
    
        # client = smtplib.SMTP_SSL()
    
        client.connect('发件人邮箱')
    
        client.login(username, password)
    
        #发件人和认证地址必须一致
    
        client.sendmail(username, rcptlist, msg.as_string())
    
        client.quit()
    
        print ('邮件发送成功!')
    
    except smtplib.SMTPRecipientsRefused:
    
        print ('邮件发送失败,收件人被拒绝')
    
    except smtplib.SMTPAuthenticationError:
    
        print ('邮件发送失败,认证错误')
    
    except smtplib.SMTPSenderRefused:
    
        print ('邮件发送失败,发件人被拒绝')
    
    except smtplib.SMTPException as e:
    
        print ('邮件发送失败, ', e.message)
    
    

    第二种,通过SDK发邮件

    控制台链接发送邮箱服务器发邮箱,该方式不能发附件

    <?php
    
    include_once'aliyun-php-sdk-core/Config.php';
    
        use Dm\Request\V20151123 as Dm;           
    
        //需要设置对应的region名称,如华东1(杭州)设为cn-hangzhou,新加坡Region设为ap-southeast-1,澳洲Region设为ap-southeast-2。
    
    $iClientProfile= DefaultProfile::getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");       
    
        //新加坡或澳洲region需要设置服务器地址,华东1(杭州)不需要设置。
    
        //$iClientProfile::addEndpoint("ap-southeast-1","ap-southeast-1","Dm","dm.ap-southeast-1.aliyuncs.com");
    
        //$iClientProfile::addEndpoint("ap-southeast-2","ap-southeast-2","Dm","dm.ap-southeast-2.aliyuncs.com");
    
    $client= new DefaultAcsClient($iClientProfile);   
    
    $request= new Dm\SingleSendMailRequest();   
    
        //新加坡或澳洲region需要设置SDK的版本,华东1(杭州)不需要设置。
    
        //$request->setVersion("2017-06-22");
    
    $request->setAccountName("控制台创建的发信地址");
    
    $request->setFromAlias("发信人昵称");
    
    $request->setAddressType(1);
    
    $request->setTagName("控制台创建的标签");
    
    $request->setReplyToAddress("true");
    
    $request->setToAddress("目标地址");
    
        //可以给多个收件人发送邮件,收件人之间用逗号分开,若调用模板批量发信建议使用BatchSendMailRequest方式
    
        //$request->setToAddress("邮箱1,邮箱2");
    
    $request->setSubject("邮件主题");
    
    $request->setHtmlBody("邮件正文");       
    
        try {
    
    $response=$client->getAcsResponse($request);
    
    print_r($response);
    
        }
    
        catch (ClientException$e) {
    
    print_r($e->getErrorCode()); 
    
    print_r($e->getErrorMessage()); 
    
        }
    
        catch (ServerException$e) {       
    
    print_r($e->getErrorCode()); 
    
    print_r($e->getErrorMessage());
    
        }
    
    ?>
    

    第三种,通过MIME加附件发邮件

    通过MIME增加附件发邮件,走SMTP先链接阿里云的邮箱服务器,再通过该邮箱服务器发邮件

    啥都不说,以码会友,python代码如下

    # -*- coding:utf-8 -*-
    
    import urllib, urllib2
    
    import smtplib
    
    from email.mime.multipartimport MIMEMultipart
    
    from email.mime.text import MIMEText
    
    from email.mime.applicationimport MIMEApplication
    
    # 发件人地址,通过控制台创建的发件人地址
    
    username= 'xxx@xxx.com'
    
    # 发件人密码,通过控制台创建的发件人密码
    
    password= 'XXXXXXXX'
    
    # 收件人地址列表,支持多个收件人,最多30个
    
    rcptlist= ['to1@to.com', 'to2@to.com']
    
    receivers= ','.join(rcptlist)
    
    # 构建 multipart 的邮件消息
    
    msg= MIMEMultipart('mixed')
    
    msg['Subject'] = 'Test Email'
    
    msg['From'] = username
    
    msg['To'] = receivers
    
    # 构建 multipart/alternative 的 text/plain 部分
    
    alternative= MIMEMultipart('alternative')
    
    textplain= MIMEText('纯文本部分', _subtype='plain', _charset='UTF-8')
    
    alternative.attach(textplain)
    
    # 构建 multipart/alternative 的 text/html 部分
    
    texthtml= MIMEText('超文本部分', _subtype='html', _charset='UTF-8')
    
    alternative.attach(texthtml)
    
    # 将 alternative 加入 mixed 的内部
    
    msg.attach(alternative)
    
    # 附件类型
    
    # xlsx 类型的附件
    
    xlsxpart = MIMEApplication(open('测试文件1.xlsx', 'rb').read())
    
    xlsxpart.add_header('Content-Disposition', 'attachment', filename=Header("测试文件1.xlsx","utf-8").encode())
    
    msg.attach(xlsxpart)
    
    # jpg 类型的附件
    
    jpgpart = MIMEApplication(open('2.jpg', 'rb').read())
    
    jpgpart.add_header('Content-Disposition', 'attachment', filename=Header("2.jpg","utf-8").encode())
    
    msg.attach(jpgpart)
    
    # mp3 类型的附件
    
    mp3part = MIMEApplication(open('3.mp3', 'rb').read())
    
    mp3part.add_header('Content-Disposition', 'attachment', filename=Header("3.mp3","utf-8").encode())
    
    msg.attach(mp3part)
    
    # 发送邮件
    
    try:
    
    client = smtplib.SMTP()
    
        #python2.7以上版本,若需要使用SSL,可以这样创建client
    
        #client= smtplib.SMTP_SSL()
    
        client.connect('smtpdm.aliyun.com')
    
        client.login(username,password)
    
        #发件人和认证地址必须一致
    
    client.sendmail(username,rcptlist,msg.as_string())
    
    client.quit()
    
        print '邮件发送成功!'
    
    except smtplib.SMTPRecipientsRefused:
    
        print '邮件发送失败,收件人被拒绝'
    
    except smtplib.SMTPAuthenticationError:
    
        print '邮件发送失败,认证错误'
    
    except smtplib.SMTPSenderRefused:
    
        print '邮件发送失败,发件人被拒绝'
    
    except smtplib.SMTPException,e:
    
        print '邮件发送失败, ', e.message
    
    image

    相关文章

      网友评论

          本文标题:阿里云的三种链接邮箱服务器发邮件方式

          本文链接:https://www.haomeiwen.com/subject/wkjryqtx.html