わかりました、私は何年もの間インターネットを見てきましたが、これに対する答えを見つけることができませんでした. 私は多くの提案を試みましたが、うまくいかないようです。Python (smtplib および email モジュール) と 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
つまり、ログインには成功しましたが、メッセージの送信を試みたときに停止しました。私を困惑させることの 1 つは、それがどこを指していないかということです。また、私のコーディングはそれほど優れていない可能性があるため、サイバーいじめはありません.
どんな助けでも大歓迎です!ありがとう。