如果你有大量郵箱要批量處理他們的郵件 如果有手工處理一定會很慢
下面用python提供的郵件庫處理,
通過這個庫可以方便的發送和接收電子郵件 代碼很短
直接上代碼
發送代碼
- #!/usr/bin/env python
- # -*- coding: gbk -*-
- #導入smtplib和MIMEText
- import smtplib
- from email.mime.text import MIMEText
- #############
- #要發給誰,這裡發給2個人
- mailto_list=["[email protected]","[email protected]"]
- #####################
- #設置服務器,用戶名、口令以及郵箱的後綴
- mail_host="smtp.qq.com"
- mail_user="your qq"
- mail_pass="password"
- mail_postfix="qq.com"
- ######################
- def send_mail(to_list,sub,content):
- '''''
- to_list:發給誰
- sub:主題
- content:內容
- send_mail("[email protected]","sub","content")
- '''
- me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
- msg = MIMEText(content)
- msg['Subject'] = sub #設置主題
- msg['From'] = me #發件人
- msg['To'] = ";".join(to_list) #收件人
- try:
- s = smtplib.SMTP()
- s.connect(mail_host)
- s.login(mail_user,mail_pass)
- s.sendmail(me, to_list, msg.as_string())
- s.close()
- return True
- except Exception, e:
- print str(e)
- return False
- if __name__ == '__main__':
- if send_mail(mailto_list,"subject","content"):
- print "發送成功"
- else:
- print "發送失敗"
接收郵件 根據發件人並提取出指定郵件
- import poplib
- import string
- from email import parser
-
- host = 'pop.qq.com'
- username = '[email protected]'
- password = 'your_password'
-
- pop_conn = poplib.POP3_SSL(host)
- pop_conn.user(username)
- pop_conn.pass_(password)
-
- #從服務器獲取郵件列表:
- messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
-
- # Concat message pieces:
- messages = ["/n".join(mssg[1]) for mssg in messages]
-
- #解析郵件到email object:
- messages = [parser.Parser().parsestr(mssg) for mssg in messages]
- for message in messages:
- addrfrom = str(message.get('from'))
- try:
- addrfrom.index('[email protected]')
- start_addr = addrfrom.index('<')
- end_addr = addrfrom.index('>')
- print addrfrom[start_addr + 1 : end_addr] #如果郵件是來自金山則打印他的標題
- except ValueError:
- continue
- pop_conn.quit()