SMTP发邮件参数设置问题。。。

hello_2012222 2012-01-28 06:26:51
"""
已实现的功能:
群发邮件包括附件
附件路径从TXT文件来读
接收方邮件地址也从TXT文件来读
"""

"""
还没解决的问题
(1)邮件标题或者正文有中文,会提示错误,英文和数字则没有错误
(2)接收端显示时间不正确,显示的不是北京时间
(3)如果跟多个好友群发,收件人会看到多个地址,希望收件人只看到自己的邮箱地址
""""

#!/usr/bin/env python3
#coding: utf-8

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import datetime
import time

sender = '123456789@163.com' #发件人邮箱
smtpserver = 'smtp.163.com' #邮件服务器
username = '123456789' #发件人邮箱用户名
password = '***********' #发件人邮箱密码

mailHead = 'Mail Head' #邮件标题(出现中文会提示错误!!!!!!)
mailBody = 'Mail Body' #邮件正文(出现中文会提示错误!!!!!!)

#获取收件人地址列表
#格式为: xxx@163.com-girl
fAddrList = open("toMailAddr.txt")
toaddrList = []
for toAddr in fAddrList:
toAddr=toAddr.replace('\n', '') #去掉换行符
toaddrList.append(toAddr) #列表追加元素

msgRoot = MIMEMultipart()
attText = MIMEText(mailBody) #设置邮件正文
attText.set_charset('utf-8')
msgRoot.attach(attText) #邮件添加正文

#没有设置发邮件的时间!!!!!!
msgRoot['from'] = sender #发件人
msgRoot['Subject'] = mailHead #邮件标题

file = open("attrlist.txt") #附件列表

#构造附件
for line in file:
line=line.replace('\n', '') #去掉换行符
att = MIMEText(open(line, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment;filename='+line+''
msgRoot.attach(att) #追加附件

#连接邮件服务器
smtp = smtplib.SMTP(smtpserver, 25)
smtp.login(username, password)

#跟好友群发消息
print ('@@@@@@ start send mail\n')
for element in toaddrList:
strTmp = element
splitIndex = strTmp.index('-') #截取'-'符号前面的地址
strTmp = strTmp[0:splitIndex]
msgRoot['to'] = strTmp #收件箱地址

#收件人会在发送地址中看到多个邮箱,希望只看到自己的邮箱地址!!!!!!
smtp.sendmail(sender, strTmp, msgRoot.as_string())
print ('\n###### stop send mail')

time.sleep(5) # 休眠5秒
smtp.quit()
...全文
346 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
test111_112233 2012-01-29
  • 打赏
  • 举报
回复
同样想了解, 帮顶....
hello_2012222 2012-01-28
  • 打赏
  • 举报
回复
请熟悉SMTP发邮件的朋友帮忙介绍下, 谢谢......
hello_2012222 2012-01-28
  • 打赏
  • 举报
回复
如果邮件标题,或正文,或附件名字, 出现中文,
就会提示类似下面的错误:
Traceback (most recent call last):
File "D:\test.py", line 54, in <module>
smtp.sendmail(sender, tolist, msgRoot.as_string())
File "C:\Python32\lib\smtplib.py", line 733, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 292-293: ordinal not in range(128)
hello_2012222 2012-01-28
  • 打赏
  • 举报
回复
现在Python用的是3.2.2版

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧