- import time
- import smtplib
- import sys
- # 465 端口发信
- def sendMailSSL(mail_sender, mail_passwd, mail_rcpts, rcpt_host):
- print(mail_sender)
- print(mail_passwd)
- print(mail_rcpts)
- print(rcpt_host)
- is_conn_timout = False
- to_mx = rcpt_host + ":465"
- helo_domain = mail_sender.split('@')[1]
- msg = '''
- From: {fromaddr}
- To: {toaddr}
- Subject: coremail SA team testing' This is not a fraud, so Don't panic
- I can't send email to you ,so I try do some test
- Please dont reply this mail , and
- So Long, and Thanks for All the Fish
- .
- '''
- msg = msg.format(fromaddr=mail_sender, toaddr=mail_rcpts)
- time_begin_conn = time.time()
- try:
- server = smtplib.SMTP_SSL(to_mx, timeout=180)
- time_end_conn = time.time()
- except Exception as e:
- print("连接收信方 MX 失败: %s" % str(e))
- sys.exit(1)
- server.set_debuglevel(1)
- time_begin_ehlo = time.time()
- server.ehlo(helo_domain)
- time_begin_mail_from = time.time()
- #server.starttls()
- server.login(mail_sender, mail_passwd) # 认证阶段
- server.mail(mail_sender) # mail from 阶段
- time_begin_rcpt = time.time()
- server.rcpt(mail_rcpts) # rcpt 阶段
- time_begin_data = time.time()
- try:
- server.data(msg)
- # time_end_data = time.time()
- except Exception as e:
- print("mail cmd DATA failed:" + str(e))
- # time_end_data = time.time()
- is_conn_timout = True
- # server.close()
- time_end_data = time.time()
- if not is_conn_timout:
- server.quit()
- time_end_all = time.time()
- print("连接 MX %s 阶段用时:%fs" % (str(rcpt_host), time_end_conn - time_begin_conn))
- print("HELO 阶段用时:%fs" % (time_begin_mail_from - time_begin_ehlo))
- print("MAIL FROM 阶段用时:%fs" % (time_begin_rcpt - time_begin_mail_from))
- print("MAIL RCPT 阶段用时:%fs" % (time_begin_data - time_begin_rcpt))
- print("MAIL DATA 阶段用时:%fs" % (time_end_data - time_begin_data))
- print("整个发信用时: %fs" % (time_end_all - time_begin_conn))
- # 25 端口发信
- def sendMailTest(mail_sender, mail_passwd, mail_rcpts, rcpt_host):
- print(mail_sender)
- print(mail_passwd)
- print(mail_rcpts)
- print(rcpt_host)
- is_conn_timout = False
- to_mx = rcpt_host + ":25"
- helo_domain = mail_sender.split('@')[1]
- msg = '''
- From: {fromaddr}
- To: {toaddr}
- Subject: coremail SA team testing' This is not a fraud, so Don't panic
- I can't send email to you ,so I try do some test
- Please dont reply this mail , and
- So Long, and Thanks for All the Fish
- .
- '''
- msg = msg.format(fromaddr=mail_sender, toaddr=mail_rcpts)
- time_begin_conn = time.time()
- try:
- server = smtplib.SMTP(to_mx, timeout=180)
- time_end_conn = time.time()
- except Exception as e:
- print("连接收信方 MX 失败: %s" % str(e))
- sys.exit(1)
- server.set_debuglevel(1)
- time_begin_ehlo = time.time()
- server.ehlo(helo_domain)
- time_begin_mail_from = time.time()
- server.starttls()
- server.login(mail_sender, mail_passwd) # 认证阶段
- server.mail(mail_sender) # mail from 阶段
- time_begin_rcpt = time.time()
- # for username in mail_rcpts:
- server.rcpt(mail_rcpts) # rcpt 阶段
- time_begin_data = time.time()
- try:
- server.data(msg)
- # time_end_data = time.time()
- except Exception as e:
- print("mail cmd DATA failed:" + str(e))
- # time_end_data = time.time()
- is_conn_timout = True
- # server.close()
- time_end_data = time.time()
- if not is_conn_timout:
- server.quit()
- time_end_all = time.time()
- print("连接 MX %s 阶段用时:%fs" % (str(rcpt_host), time_end_conn - time_begin_conn))
- print("HELO 阶段用时:%fs" % (time_begin_mail_from - time_begin_ehlo))
- print("MAIL FROM 阶段用时:%fs" % (time_begin_rcpt - time_begin_mail_from))
- print("MAIL RCPT 阶段用时:%fs" % (time_begin_data - time_begin_rcpt))
- print("MAIL DATA 阶段用时:%fs" % (time_end_data - time_begin_data))
- print("整个发信用时: %fs" % (time_end_all - time_begin_conn))
- def main():
- mail_from = "[email protected]"
- mail_passwd = "1"
- mail_to = "[email protected]"
- mx_host = "192.168.213.187"
- # 25 端口发信
- #sendMailTest(mail_sender=mail_from, mail_passwd=mail_passwd, mail_rcpts=mail_to, rcpt_host=mx_host)
- # 465 端口发信
- sendMailSSL(mail_sender=mail_from, mail_passwd=mail_passwd, mail_rcpts=mail_to, rcpt_host=mx_host)
- if __name__ == "__main__":
- main()
来源: http://www.bubuko.com/infodetail-3345560.html