Fork me on GitHub

Python 实现邮箱发送邮件

Python 实现邮箱发送邮件


 

发邮件代码

send.py 代码如下:

# coding=utf-8
import smtplib,sys
from email.mime.text import MIMEText

class Msmtp():
    def __init__(self, target, subject, content):#收件人、标题、内容
        self.msg_from = 'XXXX@qq.com' # 发件人邮箱账号
        self.password = 'XXXX'        # 发件人邮箱密码
        self.sender = smtplib.SMTP_SSL("smtp.qq.com", 465)
        self.msg_to = target
        self.subject = subject
        self.content = content

    def _login(self):
        self.sender.login(self.msg_from, self.password)

    def _msg(self):
        self.msg = MIMEText(self.content)
        self.msg['Subject'] = self.subject
        self.msg['From'] = self.msg_from
        self.msg['To'] = self.msg_to

    def send_mail(self):
        self._login()
        self._msg()
        self.sender.sendmail(self.msg_from, self.msg_to, self.msg.as_string())
        self.sender.quit()



if __name__ == '__main__':
    a = Msmtp(sys.argv[1], sys.argv[2], sys.argv[3])
    a.send_mail()

 

发送邮件

调用发送邮件,需要传入三个参数(收件人邮箱,邮件标题,邮件内容)

python send.py XXX@qq.com "Test send email" "Hello, Python."

 

收件效果

 

posted @ 2020-12-07 03:45  龙凌云端  阅读(234)  评论(0编辑  收藏  举报