#-*-Coding:utf-8-*-from email.mime.text import mimetextfrom email.header import headerimport smtplibmessage = ' ' Hello, world! From My Computer "msg = mimetext (message, ' Plain ', ' utf-8 ') msg[' Subject '] = Header (" Mail from Python ", ' utf-8 ') msg[' from '] = header (' @sina. com ') msg[' to ' = Header (' receiver ', ' utf-8 ') from_addr = ' * * * * * @sina. com ' #发件邮箱password = ' * * * ' #邮箱密码to_addr = ' * @163.com ' #收件邮箱smtp_server = ' smtp.sina.com ' #SMTP服务器try: server = Smtplib. SMTP (smtp_server,25) #第二个参数为默认端口为25, some messages have special port print (' Start login ') server.login (from_addr,password) #登录邮箱 Print (' login successful ') print ("Mail start sending") Server.sendmail (from_addr,to_addr,msg.as_string ()) #将msg转化成string发出 server.quit () print ("Mail sent successfully") except Smtplib. Smtpexception as E: print ("message sent Failed", E)
Python Learning (1)--Email