Skip to content

Instantly share code, notes, and snippets.

@ranlix
Last active December 24, 2015 05:19
Show Gist options
  • Save ranlix/6749910 to your computer and use it in GitHub Desktop.
Save ranlix/6749910 to your computer and use it in GitHub Desktop.
用smtplib来登录连接邮箱,发送测试邮件到目标邮件
# -*- coding:utf-8 -*-
import smtplib
username = "*******@163.com"
password = "*********"
smtp = smtplib.SMTP() #继承SMTP()的属性
smtp.connect("smtp.163.com","25") #连接服务器和端口
mail_text = """
From: *******@163.com\r\n
To: xxxx@gmail.com\r\n
Subject: This is a email with Python\r\n
"""
try:
smtp.login(username,password)
print "Login sucess!"
except:
print "Cannot login email, please check your username and password!"
try:
smtp.sendmail("*******@163.com","xxxx@gmail.com",mail_text)
smtp.quit()
print "Sucess!"
except:
print "cannot send the mail!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment