【问题标题】:Send an email with SMTP in Python在 Python 中使用 SMTP 发送电子邮件
【发布时间】:2016-11-21 18:51:21
【问题描述】:

我正在做一个制作我自己的即时消息程序的项目,即使没有图形或任何东西,只是为了了解 python 中的内置模块。 在这里,我尝试编写一个代码,用户将输入用户想要的用户名和密码,然后将发送一封电子邮件(给用户),其中将包含一个 12 个字符的随机字符串,用户将将其输入回程序。不知何故,当我运行代码时,我的整个计算机都冻结了! 代码如下:

import smtplib
SMTPServer = smtplib.SMTP("smtp.gmail.com",587)
SMTPServer.starttls()
SMTPServer.login(USERNAME, PASSWORD)*

userEmail = raw_input("Please enter your e-mail: ")
if verifyEmail(userEmail) == False:
    while True:
        userEmail = raw_input("Error! Please enter your e-mail: ")
        if verifyEmail(userEmail) == True:
            break

randomString = generateRandomString()
message = """From: From Person <%s>
To: To Person <%s>
Subject: Ido's IM Program Registration

Your registration code is: %s
""" %(SERVEREMAIL, userEmail, randomString)

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(SERVEREMAIL, userEmail, message)
   print "Successfully sent email"
except smtplib.SMTPException:
   print "Error: unable to send email"

inputString = raw_input("Input generated code sent: ")

【问题讨论】:

  • 您麻烦地设置了与 Gmail 的 SMTP 连接,然后不要使用它来发送您的电子邮件。相反,您使用 localhost 创建一个新的 SMTP 连接并使用它。您实际上是在运行此脚本的计算机上运行邮件服务器吗?
  • 我一定误解了 SMTP 的工作方式,你的意思是我应该删除smtpObj = smtplib.SMTP('localhost') 并将下一行更改为SMTPServer.sendmail(SERVEREMAIL, userEmail, message),因为我在写@987654325 时已经创建了服务器@ ?
  • 除非您实际上是在与脚本相同的计算机上运行 SMTP 服务器,否则这听起来不错。

标签: python email crash smtp smtplib


【解决方案1】:

这是一个 smtp 客户端的工作示例。 你的代码在哪里阻塞?

# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText


class SMTPClient(object):
    def __init__(self, recepient, subject, body):
        self.recepient = recepient
        self.subject = subject
        self.body = body
        self.mail_host = conf.get('smtp_server.host')
        self.mail_port = conf.get('smtp_server.port')
        self.username = conf.get('account.username')
        self.password = conf.get('account.password')
        self.mail_sender = conf.get('account.from')
        self._setup()

    def _setup(self):
        self.smtp_client = smtplib.SMTP(self.mail_host, self.mail_port)
        self.smtp_client.ehlo_or_helo_if_needed()
        self.smtp_client.starttls()
        self.smtp_client.login(self.username, self.password)
        self._send_mail()

    def _make_mail(self):
        message = MIMEText(self.body, _charset='utf-8')
        message['From'] = self.mail_sender
        message['To'] = self.recepient
        message['Subject'] = self.subject
        return message.as_string()

    def send_mail(self):
        self.smtp_client.sendmail(self.mail_sender, self.recepient, self._make_mail())
        self.smtp_client.quit()

【讨论】:

    【解决方案2】:

    这对我有用!

    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email import Encoders
    
    gmail_user = 'example@hotmail.com'
    gmail_pwd = 'somepassword'
    subject = 'HEY'
    text = 'Some text here'
    
    msg = MIMEMultipart()
    msg['From'] = gmail_user
    msg['To'] = gmail_user
    msg['Subject'] = subject
    
    msg.attach(MIMEText(text))
    
    part = MIMEBase('application', 'octet-stream')
    
    Encoders.encode_base64(part)
    
    mailServer = smtplib.SMTP("smtp.live.com", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmail_user, gmail_pwd)
    mailServer.sendmail(gmail_user,gmail_user, msg.as_string())
    mailServer.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2015-11-10
      相关资源
      最近更新 更多