Python script to send QQ Mail continuously

Source: Internet
Author: User
Tags python script

Today learned to use Python to write a continuous send QQ mail script, after testing, successfully send mail to domestic and foreign servers, including QQ mailbox, 163 mailbox, Google Mail, the Hong Kong University of Science and Technology mailbox and the University of Edinburgh mailbox. Steps to answer the relevant tips.

First, do the following, __init__ flag the folder is a Python package

$mkdir  automail$cd automail$gedit qqmail$Touch __init__

Open Qqmail and write the following code.

1 #-*-coding:utf-8-*-2 Importsys, OS, RE, urllib, Urlparse3 ImportSmtplib4 ImportTraceback5  fromEmail.mime.textImportMimetext6  fromEmail.mime.multipartImportMimemultipart7  8 defSendMail (Subject,msg,toaddrs,fromaddr,smtpaddr,password):9     " "Ten @subject: Message subject One @msg: Message content A @toaddrs: Email address of the person receiving the letter - @fromaddr: Email address of sender - @smtpaddr: SMTP Service address, can be seen in the mailbox, such as 163 mailbox for smtp.163.com the @password: Sender's email password -     " " -Mail_msg =Mimemultipart () -     if  notisinstance (subject,unicode): +Subject = Unicode (subject,'Utf-8') -mail_msg['Subject'] =subject +mail_msg[' from'] =fromaddr Amail_msg[' to'] =Toaddrs atMail_msg.attach (Mimetext (MSG,'Plain','Utf-8')) -     #Mail_msg.attach (Mimetext (msg, ' html ', ' Utf-8 ')) -     Try: -s =Smtplib. SMTP () -S.connect (SMTPADDR)#connecting to an SMTP server -S.login (Fromaddr,password)#Login Mailbox inS.sendmail (Fromaddr, Toaddrs, mail_msg.as_string ())#Send mail -         Print "Mail sent to%s succeed!"%(Toaddrs) to s.quit () +     exceptexception,e: -        Print "error:unable to send e-mail to%s!"%(Toaddrs) the        PrintTraceback.format_exc () *   $ if __name__=='__main__':Panax NotoginsengFROMADDR ="[email protected]" -SMTPADDR ="smtp.qq.com" +Subject ="Robio2018" APassword ="Svwpwgunhrbpbjha" theFile_object = open (' Letter') +msg =File_object.read () - file_object.close () $      forToaddrsinchOpen"NameList"): $Prof =Toaddrs -SendMail (Subject,msg,prof,fromaddr,smtpaddr,password)

Code interpretation:

    • Import Email Import Smtplib

      SMTP is the protocol that sends messages, and Python's built-in support for SMTP can send plain text messages ,HTML messages , and messages with attachments . Python supports SMTP support with Smtplib and email two modules, email is responsible for structuring the mail, Smtplib is responsible for sending mail.

    •  def  sendmail (Subject,msg,toaddrs,fromaddr,smtpaddr,password) The most core code of the 

      is the function that correctly fills in the function's arguments, sets the mailbox to allow third-party operations.

    •  Mail_msg.attach (Mimetext (msg,  " plain  , "  utf-8   )) 

      Line 23rd, the first parameter is the message body content, the second argument ' plain ', because we send a plain text message, the third parameter guarantees the compatibility of multiple languages. For more information about sending multiple types of messages, refer to the Liaoche Teacher's tutorial.

    • File_object = open ('letter'= file_object.read () file_object.close ()

      Code 42-44, open the letter document in the same path as the script, read the entire content and store it in a large MSG string, close the document in a timely manner, or it will always occupy memory. If it is troublesome to manually turn off each time, Python has a with keyword to automatically close the document, please Baidu itself.

    •  for  in open ("namelist")

      Line 45th, use a For loop to read the recipient mailbox line by row from the recipient's mailbox list.

Python script to send QQ Mail continuously

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.