Python3 implement 126 mailbox 163 mailbox SMTP Send mail

Source: Internet
Author: User

e-mail to remind the role, when the crawler encountered an exception or the server encountered problems, you can report to yourself in a timely manner by email.
The protocol for sending mail is Stmp,python built-in support for SMTP, which can send plain text messages, HTML messages, and messages with attachments.

SMTP protocol

First understand SMTP (Simple Mail Transfer Protocol), the mail delivery Agent uses the SMTP protocol to send email to the recipient's mail server. The SMTP protocol can only be used to send messages, not to receive messages, and most mail-sending servers use the SMTP protocol. The default TCP port number for the SMTP protocol is 25.

Configuration before sending the message:

It says that the mail is sent using the SMTP protocol, so you need to check whether your sender's mailbox has an SMTP protocol enabled, if not, it needs to be turned on, I test using a 163.com mailbox as the sender's mailbox, the SMTP protocol is turned on in the settings as shown in.

Construct a simple plain text message

From Email.mime.text import Mimetext
msg = mimetext (' Python crawler run exception, exception information for encountering HTTP 403 ', ' plain ', ' utf-8 ')
Three parameters required to construct a Mimetext object
Message body, ' Python crawler running exception, exception information for encountering HTTP 403 '
Mimel's subtype, ' plain ' represents plain text
encoded format, ' Utf-8 '

Example (1)

ImportSmtplib fromEmail.headerImportHeader fromEmail.mime.textImportMimetext#third-party SMTP servicesMail_host ="smtp.126.com"      #SMTP ServerMail_user ="[email protected]"                  #User nameMail_pass ="******"               #authorization password, non-login passwordSender='[email protected]'    #Sender's mailbox (preferably write-all, otherwise it will fail)receivers = ['[email protected]']#receive mail, can be set as your QQ mailbox or other mailboxcontent='python crawler Run exception'title='Test'  #Message Subject defsendEmail (): Message= Mimetext (Content,'Plain','Utf-8')#content, format, encodingmessage[' from'] ="{}". Format (sender) message[' to'] =",". Join (receivers) message['Subject'] =titleTry: Smtpobj= Smtplib. Smtp_ssl (Mail_host, 465)#enable SSL send, port is generally 465Smtpobj.login (Mail_user, Mail_pass)#Login VerificationSmtpobj.sendmail (sender, Receivers, message.as_string ())#Send        Print("Mail has been send successfully.")    exceptSmtplib. Smtpexception as E:Print(e)defSend_email2 (Smtp_host, From_account, FROM_PASSWD, To_account, subject, content): Email_client=Smtplib. SMTP (smtp_host) email_client.login (From_account, from_passwd)#Create msgmsg = Mimetext (content,'Plain','Utf-8') msg['Subject'] = Header (subject,'Utf-8')#subjectmsg[' from'] =From_account msg[' to'] =To_account email_client.sendmail (From_account, To_account, msg.as_string ()) Email_client.quit ()if __name__=='__main__': SendEmail ()

(2)

1 ImportSmtplib2  fromEmail.headerImportHeader3  fromEmail.mime.textImportMimetext4  5 #third-party SMTP services6Mail_host ="smtp.126.com"      #SMTP Server7Mail_user ="[email protected]"                  #User name8Mail_pass ="******"               #authorization password, non-login password9  TenSender ='[email protected]'    #Sender's mailbox (preferably write-all, otherwise it will fail) Onereceivers = ['[email protected]']#receive mail, can be set as your QQ mailbox or other mailbox A   -Content ='smart, you tell me, why are our days gone? ' -title ='hurried'  #Message Subject the   - defsendEmail (): -   -message = Mimetext (content,'Plain','Utf-8')#content, format, encoding +message[' from'] ="{}". Format (sender) -message[' to'] =",". Join (receivers) +message['Subject'] =title A   at     Try: -Smtpobj = Smtplib. Smtp_ssl (Mail_host, 465)#enable SSL send, port is generally 465 -Smtpobj.login (Mail_user, Mail_pass)#Login Verification -Smtpobj.sendmail (sender, Receivers, message.as_string ())#Send -         Print("Mail has been send successfully.") -     exceptSmtplib. Smtpexception as E: in         Print(e) -   to defSend_email2 (Smtp_host, From_account, FROM_PASSWD, To_account, subject, content): +Email_client =Smtplib. SMTP (smtp_host) - Email_client.login (From_account, from_passwd) the     #Create msg *msg = Mimetext (content,'Plain','Utf-8') $msg['Subject'] = Header (subject,'Utf-8')#subjectPanax Notoginsengmsg[' from'] =From_account -msg[' to'] =To_account the Email_client.sendmail (From_account, To_account, msg.as_string ()) +   A email_client.quit () the   + if __name__=='__main__': - SendEmail () $     #receiver = ' * * * ' $     #Send_email2 (Mail_host, Mail_user, Mail_pass, receiver, title, content)

The terminal displays a successful send.

1 wljdemacbook-pro:desktop wlj$ python3 126. py2Mail has been send successfully. 3

If the test error, please see the NetEase mailbox given the status code and meaning.

Python3 implement 126 mailbox 163 mailbox SMTP Send mail

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.