好吧,伙计们,我在互联网上看了很长时间,根本找不到答案。我已经尝试了很多建议,但我似乎无法让它发挥作用。我正在尝试使用 python(smtplib 和电子邮件模块)和 gmail 服务发送电子邮件。这是我导入的包:
import time, math, urllib2, urllib, os, shutil, zipfile, smtplib, sys
from email.mime.text import MIMEText
这是我发送电子邮件的 def 声明:
def sendmessage():
print('== You are now sending an email to Hoxie. Please write your username below. ==')
mcusername = str(raw_input('>> Username: '))
print('>> Now your message.')
message = str(raw_input('>> Message: '))
print('>> Attempting connection to email host...')
fromaddr = 'x@gmail.com'
toaddrs = 'xx@gmail.com'
username = 'x@gmail.com'
password = '1013513403'
server = smtplib.SMTP('smtp.gmail.com:587')
subject = 'Email from',mcusername
content = message
msg = MIMEText(content)
msg['From'] = fromaddr
msg['To'] = toaddrs
msg['Subject'] = subject
try:
server.ehlo()
server.starttls()
server.ehlo()
except:
print('!! Could not connect to email host! Check internet connection! !!')
os.system('pause')
main()
else:
print('>> Connected to email host! Attempting secure login via SMTP...')
try:
server.login(username,password)
except:
print('!! Could not secure connection! Stopping! !!')
os.system('pause')
main()
else:
print('>> Login succeeded! Attempting to send message...')
try:
server.sendmail(fromaddr, toaddrs, msg)
except TypeError as e:
print e
print('Error!:', sys.exc_info()[0])
print('!! Could not send message! Check internet connection! !!')
os.system('pause')
main()
else:
server.quit()
print('>> Message successfully sent! I will respond as soon as possible!')
os.system('pause')
main()
我已经尽可能广泛地调试并得到了这个:
>> Login succeeded! Attempting to send message...
TypeError: expected string or buffer
这意味着它成功登录,但在尝试发送消息时停止。让我感到困惑的一件事是它没有指出哪里。此外,我的编码可能不是那么好,所以没有网络欺凌。
任何帮助将不胜感激!谢谢。