码迷,mamicode.com
首页 > 编程语言 > 详细

python实现发送邮件

时间:2015-10-25 13:41:50      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

下面的程序利用smtp协议实线邮件的发送。接下来会优化使得也能够利用pop3或者imap协议实现邮件的收取。

#/usr/bin/env python
#-*- coding:utf8 -*-

import smtplib  
from email.mime.text import MIMEText

class Mail_helper:

    def __init__(self):
        self.username = raw_input(‘username: ‘)
        self.password = raw_input(‘password: ‘)

    def send(self):
        host = ‘smtp.163.com‘
        port = 25
        receiver = raw_input(‘send to: ‘)
        title = raw_input(‘title: ‘)
        body = ‘<p>‘+raw_input(‘content: ‘)+‘</p>‘

        msg = MIMEText(body, ‘html‘)
        msg[‘subject‘] = title
        msg[‘from‘] = self.username
        msg[‘to‘] = receiver

        s = smtplib.SMTP(host, port)
        s.login(self.username, self.password)
        s.sendmail(self.username, receiver, msg.as_string())

        print ‘mail has been sent.‘

if __name__ == ‘__main__‘:
    mailer = Mail_helper()
    mailer.send()

 

python实现发送邮件

标签:

原文地址:http://www.cnblogs.com/wuyouwulv/p/python_send_email.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!