Use python to send emails

Source: Internet
Author: User

The smtplib module in python can use the SMTP protocol to send emails,

You can send a message to a specified mailbox after setting the sender, recipient, topic (can be empty), and message (can be empty.

After testing, it is more convenient.

Test code:

# MailClient class from smtplib import SMTPfrom time import *class MailClient:    def __init__(self):        self.SMTPServer = 'smtp.qq.com' #send server        self.SvrSuffix  = 'qq.com'        self.user = 'username' #user name        pnum = '12345678' #password        self.sendSvr = SMTP(self.SMTPServer)        self.log = self.sendSvr.login(self.user,pnum)#login the server                #print 'connect to Mail Server successfully.'            def SendMail(self,des, subj=None,info=None,PrintResult=False):        '''        mail info to des through default server        des: user@server.com        subj:mail subject        info:list or tuple of information to send        PrintResult: whether print the send result        '''        SrcInfo = 'From: '+self.user+'@'+self.SvrSuffix        To = 'To: '+str(des)        Sub = 'Subject: '+str(subj)        Header = [SrcInfo,To,Sub] #mail header        Msg = '\r\n\r\n'.join(['\r\n'.join(Header),'\r\n'.join(info)])        self.errs = self.sendSvr.sendmail(self.user+'@'+self.SvrSuffix,des,Msg)        #self.sendSvr.quit()        if PrintResult==False:            return        if(len(self.errs)!=0):            print "error info : "            for e in self.errs:                print e," ",self.errs[e]                return -1        else:            print "send to ",des,":"            for e in info:                print "  ",e            print "successfully."            return 0    def __del__(self):        #print 'deconstructor'        self.sendSvr.quit()        #print 'MailClient disconnected.'if __name__=='__main__':    b = time()    mc = MailClient()    s = 'null subject 2'    Text = ('Hello from your friend ','Best wishes.')    e = time()    print 'time 1 ',e-b    b=e    mc.SendMail('xxx@qq.com',s,Text,True)    e = time()    print 'time 2 ',e-b    b=e        mc.SendMail('yyy@qq.com',s,Text*2,True)    e = time()    del mc    print 'time cost ',e-b,'s.'

Create a mailclient object before using it, and then call the Sendmail method of the object (parameters include receiver, topic, message list, and message sending Result Display switch ).

You only need to call this method multiple times when sending it to different addresses.

[In fact, in a single sending process, the receiving address in the receiving mail list can also be multiple, but there are some problems during the test, the list is relatively small (only a few) There is no problem, the server rejects sending dozens of requests.]

The following is a test code:

from time import *begin = time()##################################revr=[]##reveiveer listrevr.append('666666666@qq.com') #just for test start = 666666667num = 10while num>0:    revr.append(str(start)+'@qq.com')    start = start+1    num = num-1#for e in revr:#    print efrom MailClient import *mc = MailClient()errmail=[]for user in revr:    s = 'null subject'    Text = ('Hello from your friend ','Best wishes.')    try:        mc.SendMail(user,s,Text)    except:        #print "exception at ",user        errmail.append(user)del mcprint "Failed list(",len(errmail),"):"for e in errmail:    print eend = time()print "time cost: ",(end-begin)," s."#sleep(10)

Modify the number of sent mails to send emails to all the QQ numbers in a region. This is basically the reason for spam text messages and spam.


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.