Python Send mail

Source: Internet
Author: User
Tags email account mail account

Send a simple message

Send mail using the SMTP service of a third-party mail provider (SMTP service using the QQ mailbox in this article)

Authorization Code acquisition: Login QQ Mailbox-click Settings, click on the Account->pop3/imap/smtp.../caldav service menu click Generate Authorization Code, the generated authorization code as the password

ImportSmtplib fromEmail.mime.textImportMimetext fromEmail.utilsImportFormataddrsend_mail='[email protected]'  #Sender Email AccountSend_pwd ='xxxxxxx'    #Sender Mailbox Password Authorization CodeReceiv_mail ='[email protected]'  #Recipient's email accountmsg= Mimetext ('This is a test e-mail','Plain','Utf-8')#message bodymsg[' from'] = FORMATADDR (['fromxxx', Send_mail])#Sender Nickname, email accountmsg[' to'] = FORMATADDR ([' to', Receiv_mail])#recipient nickname, email accountmsg['Subject'] ='Test Message'         #Message SubjectServer = Smtplib. Smtp_ssl ('smtp.qq.com', 465)#SMTP server in the sender's mailbox with port number 465Server.login (SEND_MAIL,SEND_PWD)#E- mail account number, passwordServer.sendmail (send_mail,[receiv_mail,],msg.as_string ())#Send mailServer.quit ()#Close Connection
Send HTML-formatted messages
ImportSmtplib fromEmail.mime.textImportMimetext fromEmail.headerImportHeadersend_mail='[email protected]'send_pwd='xxxxx'Receiv_mail='[email protected]'mail_msg=" "<p> This is a test message </p><p><a href= "" > Point me Activation </a></p>" "msg= Mimetext (Mail_msg,'HTML','Utf-8') msg[' from'] = Header ('Test Message','Utf-8') msg[' to'] = Header ('Test','Utf-8') Subject='Send HTML-formatted messages'msg['Subject'] = Header (subject,'Utf-8') Server= Smtplib. Smtp_ssl ('smtp.qq.com', 465) Server.login (send_mail,send_pwd) Server.sendmail (send_mail,[receiv_mail,],msg.as_string ()) Server.quit () 
Send a message with an attachment
ImportSmtplib fromEmail.mime.textImportMimetext fromEmail.headerImportHeader fromEmail.mime.multipartImportMimemultipartsend_mail='[email protected]'send_pwd='xxxxx'Receive_mail='xxxxx.com'msg=Mimemultipart () msg[' from'] = Header ('Test Message','Utf-8') msg[' to'] = Header ('Test','Utf-8') Subject='Mail'msg['Subject'] = Header (subject,'Utf-8') Msg.attach (Mimetext ('This is an e-mail for the test attachment.','Plain','Utf-8'))#message bodyatt1= Mimetext (Open ('Text.txt','RB'). Read (),'Base64','Utf-8')#Annex 1att1['Content-type'] ='Application/octet-stream'att1['content-disposition'] ='attachment;filename= "Text.txt"'Msg.attach (ATT1) Server= Smtplib. Smtp_ssl ('smtp.qq.com', 465) Server.login (send_mail,send_pwd) Server.sendmail (send_mail,[receive_mail,],msg.as_string ()) Server.quit () 
Add a picture in HTML
ImportSmtplib fromEmail.mime.textImportMimetext fromEmail.headerImportHeader fromEmail.mime.imageImportMimeimage fromEmail.mime.multipartImportMimemultipartsend_mail='[email protected]'send_pwd='xxxxx'Receiv_mail='[email protected]'msg= Mimemultipart ('related') msg[' from'] = Header ('Test Message','Utf-8') msg[' to'] = Header ('Test','Utf-8') Subject='Send HTML-formatted messages'msg['Subject'] = Header (subject,'Utf-8') msgalternative= Mimemultipart ('Alternative') Msg.attach (msgalternative) mail_msg=" "<p> This is a test message to add a picture </p><p> photo demo: </p><p></p> " "Msgalternative.attach (Mimetext (mail_msg,'HTML','Utf-8')) pic= Open ('123.png','RB') Msgimage=Mimeimage (Pic.read ()) Pic.close () Msgimage.add_header ('Content-id','<image1>') Msg.attach (msgimage) Server= Smtplib. Smtp_ssl ('smtp.qq.com', 465) Server.login (send_mail,send_pwd) Server.sendmail (send_mail,[receiv_mail,],msg.as_string ()) Server.quit () 

Python Send mail

Related Article

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.