Sending an Email with python using the SMTP Library
import smtplib
def prompt(prompt):
return raw_input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
msg = ("From: %s\r开发者_StackOverflow\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line
print "Message length is " + repr(len(msg))
server = smtplib.SMTP('smtp.live.com:25')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
I'm trying to send an Email using that example, but it doesnt work, I dont get any error it just doesn't send anything, I'm trying to send it to a hotmail email. Any help will be appreciated, Thanks.
Your call to smtplib.SMTP()
uses invalid arguments. According to the smtplib docs:
SMTP([host[, port[, local_hostname[, timeout]]]])
So, the SMTP
constructor takes the optional arguments for hostname, port etc. But you've passed port and hostname as one argument (server = smtplib.SMTP('smtp.live.com:25')
).
Provided you have everything else right, if you change that line to read server = smtplib.SMTP('smtp.live.com', 25)
.
If your server requires authentication (and I suspect it does), before you actually send the email you'll want to call server.login(user, password)
to login so you can actually send the message.
精彩评论