繁体   English   中英

在Python中使用SMTP发送电子邮件

[英]Send an email with SMTP in Python

我正在开发一个自己的即时消息传递程序的项目,即使没有图形或任何东西,也只是为了了解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: ")

这是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()

这对我有用!

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM