本地测试发邮件功能很流畅,部署到阿里云上以后发现总是NOT FIND,这就很奇怪。开始以为是url写错了导致的,检查N多遍发现完全一毛一样的。后来各种百度,发现是因为阿里云禁用了25端口导致的。查看各种资料,解决的办法五花八门。试了几种解决方案,都解决了问题。现在整理如下:(不用去尝试申请解禁25端口的,可以很认真负责的告诉你,完全没有卵用)
首先,是阿里大大给的官方的解决方案,用SMTP发送邮件:代码如下
# -*- 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 # 发件人地址,通过控制台创建的发件人地址 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() #python 2.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