1、登录SMTP服务器
首先使用网上的方法(这里使用163邮箱,smtp.163.com是smtp服务器地址,25为端口号):
import smtplib server = smtplib.SMTP('smtp.163.com', 25) server.login('j_hao104@163.com', 'password') Traceback (most recent call last): File "C:/python/t.py", line 192, in <module> server.login('j_hao104@163.com', 'password') File "C:\Python27\lib\smtplib.py", line 622, in login raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed')
发现返回:
smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed')
,提示验证失败。
有说python不支持SMTP服务,或是服务没开启之类的。但是我想起上次我用foxmail登录我的163邮箱的时候,邮箱密码都输对了还是提示我密码错误,最后的解决办法是:像QQ和163邮箱现在都有个客户端密码,用第三方登录时需用客户端密码登录才行,python也是如此,因此去设置好客户端密码,再用客户端密码登录。
import smtplib server = smtplib.SMTP('smtp.163.com', 25) server.login('j_hao104@163.com', 'clientPassword')
此时便返回登录成功提示:
(235, 'Authentication successful')
2、发送邮件
首先使用网上给出的代码:
import smtplib from email.mime.text import MIMEText server = smtplib.SMTP('smtp.163.com', 25) server.login('j_hao104@163.com', 'clientPassword') msg = MIMEText('hello, send by Python...', 'plain', 'utf-8') server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string())
构造MIMEText对象时,第一个参数是邮件正文,第二个参数是MIME的subtype,最后个是编码方式。
sendmail是发邮件方法,第一个参数是发件邮箱,第二个参数是收件人邮箱,是一个列表,代表可以同时发给多个人,as_string是把MIMEText对象变成str。
但是执行结果并不能得到网上说的结果:
而是:
Traceback (most recent call last): File "C:/python/t.py", line 195, in <module> server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string()) File "C:\Python27\lib\smtplib.py", line 746, in sendmail raise SMTPDataError(code, resp) smtplib.SMTPDataError: (554, 'DT:SPM 163 smtp11,D8CowEDpDkE427JW_wQIAA--.4996S2 1454562105,please see http://mail.163.com/help/help_spam_16.htm?ip=171.221.144.51&hostid=smtp11&time=1454562105')
网上一查才知道:smtplib.SMTPDataError: (554, 'DT:SPM 163 smtp11……的错误是因为信封发件人和信头发件人不匹配。可以看出看出图片中并没有发件人和主题,所以需要对代码做如下修改:
import smtplib from email.header import Header from email.mime.text import MIMEText server = smtplib.SMTP('smtp.163.com', 25) server.login('j_hao104@163.com', 'clientPassword') msg = MIMEText('hello, send by Python...', 'plain', 'utf-8') msg['From'] = 'j_hao104@163.com <j_hao104@163.com>' msg['Subject'] = Header(u'text', 'utf8').encode() msg['To'] = u'飞轮海 <jinghao5849312@qq.com>' server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string())
这样就能成功发出邮件啦
msg里的具体信息可以用一般发邮件方式发封邮件测试下
3、参考示例
import smtplib from email.mime.text import MIMEText to_list = ['123@123.com', '456@456.com'] server_host = 'smtp.163.com' username = '你的邮箱账号' password = '你的邮箱密码' def send(to_list, sub, content): ''' :param to_list: 收件人邮箱 :param sub: 邮件标题 :param content: 内容 ''' me = "manager" + "<" + username + ">" # _subtype 可以设为html,默认是plain msg = MIMEText(content, _subtype='html') msg['Subject'] = sub msg['From'] = me msg['To'] = ';'.join(to_list) try: server = smtplib.SMTP() server.connect(server_host) server.login(username, password) server.sendmail(me, to_list, msg.as_string()) server.close() except Exception as e: print str(e) if __name__ == '__main__': send(to_list, "这个是一个邮件", "<h1 id="Hello-It-s-test-email">Hello, It's test email.</h1>")

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。