Email - 发送邮件

import sys
import socket
import smtplib

server = 'smtp.163.com'  #SMTP服务器
fromaddr = ******@***.com'  #接收邮箱
toaddrs = '******@***.com.cn' #发送邮箱

message = """ To :%s
From:%s
Subject:Test Message
Hello""" % (','.join(toaddrs), fromaddr)

try:
    s = smtplib.SMTP(server)  #SMTP核心类
    s.set_debuglevel(1)
    s.login('******', '******') #SMTP认证
    s.sendmail(fromaddr, toaddrs, message)
    print 'Message successfylly sent to %d recipient(s)' % len(toaddrs)
except (socket.gaierror, socket.error, socket.herror, \
        smtplib.SMTPException), e:
    print 'Your message may not have been sent!'
    print e
    sys.exit(1)

你可能感兴趣的:(socket)