使用python进行发送邮件,研究的主要是用smtplib这个包,具体代码如下,eg:
#!/usr/bin/python #coding=utf-8 import smtplib from email.mime.text import MIMEText def sm(receiver, title, body): host = 'smtp.163.com' port = 25 sender = '*****@163.com' pwd = '********' msg = MIMEText(body, 'html') msg['subject'] = title msg['from'] = sender msg['to'] = receiver s = smtplib.SMTP(host, port) s.login(sender, pwd) s.sendmail(sender, receiver, msg.as_string()) print 'The mail named %s to %s is sended successly.' % (title, receiver) sm('****@qq.com','Hello,World','This is test send email')
这里需要说的是为什么使用163的邮箱,因为我刚开始在这里登录邮箱是使用qq邮箱,结果使用qq邮箱很久都没有成功,报各种错误,然后找了一下可能是smtp的原因,换成163邮箱果断成功了。