Python process for sending e-mail using the Smtplib module

Source: Internet
Author: User
Tags email account
1. Log in to the SMTP server
First use the online method (here use 163 mailbox, smtp.163.com is the SMTP server address, 25 is the port number):

Import Smtplibserver = Smtplib. SMTP (' smtp.163.com ', ' Server.login ') (' j_hao104@163.com ', ' Password ') Traceback (most recent call last): File "C:/python /t.py ", line 192, in 
 
  
   
    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 ')
 
  

Found to return:

Smtplib. Smtpauthenticationerror: (535, ' error:authentication failed ')

, prompting for validation failure.
Python does not support the SMTP service, or the service does not open. But I think of the last time I used foxmail login to my 163 mailbox, the mailbox password is the right or prompt me password error, the Final solution is: like QQ and 163 mailbox now have a client password, with a third-party login with a client password to log on only line, Python is so, So go to set the client password, and then log in with the client password.

Import Smtplibserver = Smtplib. SMTP (' smtp.163.com ', +) server.login (' j_hao104@163.com ', ' Clientpassword ')

This returns the login success prompt:

(235, ' authentication successful ')

2. Send mail

First use the code given online:

Import smtplibfrom email.mime.text Import mimetextserver = Smtplib. SMTP (' smtp.163.com ', ' 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 ())

When constructing a Mimetext object, the first parameter is the message body, the second parameter is the MIME subtype, and the last is the encoding method.
SendMail is the e-mail method, the first parameter is the sender mailbox, the second parameter is the recipient mailbox, is a list, the representative can be sent to more than one person, as_string is the Mimetext object into Str.
But the results do not get the results online:

But:

Traceback (most recent): File "c:/python/t.py", line 195, in 
 
  
   
    server.sendmail (' j_hao104@163.com ', [' 946150454@qq.com '], msg.as_string ()) File "C:\Python27\lib\smtplib.py", line 746, 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 ')
 
  

Online A check only know: Smtplib. Smtpdataerror: (554, ' DT:SPM 163 smtp11 ... The error is because the envelope sender and the letter hair person do not match. You can see that there are no senders and themes in the picture, so you need to make the following modifications to the code:

Import smtplibfrom email.header import headerfrom email.mime.text Import mimetextserver = Smtplib. SMTP (' smtp.163.com ', ' Server.login ') (' j_hao104@163.com ', ' clientpassword ') msg = Mimetext (' Hello, send by Python ... '), ' Plain ', ' utf-8 ') msg[' from '] = ' j_hao104@163.com 
 
  
   
  ' msg[' Subject '] = Header (U ' text ', ' UTF8 '). Encode () msg[' to '] = U ' Flywheel sea 
  
   
    
   ' server.sendmail (' j_hao104@163.com ', [' 946150454@qq.com '], msg.as_string ())
  
   
 
  

So you can send a successful email.
The specific information in MSG can be sent under the general e-mail message test

3. Reference example

Import smtplibfrom email.mime.text Import mimetextto_list = [' 123@123.com ', ' 456@456.com ']server_host = ' smtp.163.com ' Username = ' Your email account ' Password = ' Your mailbox password ' def send (to_list, sub, content): ' '  :p Aram to_list: Recipient mailbox  :p Aram Sub: Message title  :p Aram Content: Contents "  me =" Manager "+" < "+ username +" > "   # _subtype can be set to HTML, default is plain  m sg = 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, "This is a mail", "

Hello, It ' s test email.

")
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.