2016-11-21 68 views
0

我正在開發一個製作我自己的即時消息程序的項目,即使沒有圖形或任何東西,也只是爲了瞭解Python中的內置模塊。 在這裏,我試圖編寫一個代碼,用戶將輸入用戶所需的用戶名和密碼,然後將發送一封電子郵件給用戶,該電子郵件將包含一個12個字符的隨機字符串,用戶將輸入回程序。不知何故,當我運行代碼時,我的整個計算機都凍結了! 下面的代碼:在Python中發送一封包含SMTP的電子郵件

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: ") 
+0

你不得不使用它來發送電子郵件。而是使用'localhost'創建一個新的SMTP連接並使用它。您是否真的在運行此腳本的計算機上運行郵件服務器? – kindall

+0

我一定誤解了SMTP的工作方式,您的意思是我應該刪除'smtpObj = smtplib.SMTP('localhost')'並將下一行更改爲'SMTPServer.sendmail(SERVEREMAIL,userEmail,message)',因爲當我編寫'SMTPServer = smtplib.SMTP(「smtp.gmail.com」,587)''時,我已經創建了服務器? –

+0

除非您真的在與腳本相同的計算機上運行SMTP服務器,否則這聽起來像是一件好事。 – kindall

回答

0

這是一個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() 
0

這適用於我!

import smtplib 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEBase import MIMEBase 
from email.MIMEText import MIMEText 
from email import Encoders 

gmail_user = '[email protected]' 
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()