0
import smtplib
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login('[email protected]', 'myPassword')
file = 'C:\\Users\\PC1\\Desktop\\myFile.txt'
f = open(file, "r")
filecontent = (f.read())
server.sendmail('[email protected]', '[email protected]', filecontent)
當我運行此代碼時,出現如下錯誤;發送帶附件的電子郵件
Traceback (most recent call last):
File "C:\Users\PC1\Desktop\pyos\mail.py", line 10, in <module>
server.sendmail('[email protected]', '[email protected]', filecontent)
File "C:\Users\PC1\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 854, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u0131' in position 31: ordinal not in range(128)
我該如何克服這一點?我不想在我發送的文件中出現字符問題。
你正在試圖用你需要適應的編碼字符的文件中讀取。這已被回答[之前](https://stackoverflow.com/questions/11086752/read-a-text-file-with-non-ascii-characters-in-an-unknown-encoding) –
對不起,這不是特定的在所有。 –