如何使用python在hotmail中编写邮件?

时间:2011-07-25 07:03:04

标签: python email hotmail

import poplib

M = poplib.POP3_SSL('pop3.live.com', 995) #Connect to hotmail pop3 server

try: 

    M.user(raw_input("username: ")) #Get the username from the standar input
    M.pass_(raw_input("password: ")) #Get the password from the standar input
except:

    print "username or password incorrect"
else:

    print "Successful login"

import smtplib

msg = "warning"

msg['From'] = "capstons2011jm4@hotmail.com"

msg['To'] = "yuxun88@hotmail.com"

msg['Subject'] = "hello"

s = smtplib.SMTP("smtp.live.com",25)

s.sendmail("capstones2011jm4@hotmail.com", "yuxun88@hotmail.com", msg.as_string())

s.quit()

我发现如何使用python登录hotmail。

但我仍然无法在hotmail中发送电子邮件。

TypeError: 'str' object does not support item assignment  This keep coming up. I have no idea why.

有谁知道如何编写以下代码。 Plz的帮助。我会很感激的。

5 个答案:

答案 0 :(得分:1)

问题就在这里:

msg = "warning"
msg['From'] = "capstons2011jm4@hotmail.com"
msg['To'] = "yuxun88@hotmail.com"
msg['Subject'] = "hello"

msg是str,您试图将其视为字典,并尝试为其分配值。这是错误的。

您遇到的错误是试图说您无法将值分配给字符串中的索引位置。

答案 1 :(得分:0)

请参阅email包的文档:http://docs.python.org/library/email.html

有很多examples

答案 2 :(得分:0)

看起来你需要email模块:

>>> import email
>>> msg = email.message_from_string('warning')
>>> msg['From'] = "capstons2011jm4@hotmail.com"
>>> msg['To'] = "yuxun88@hotmail.com"
>>> msg['Subject'] = "hello"
>>> print msg.as_string()
From: capstons2011jm4@hotmail.com
To: yuxun88@hotmail.com
Subject: hello

warning

答案 3 :(得分:0)

我想你可能会看看这个模块:

http://docs.python.org/library/email.message.html

它解释得很好! 我过去使用的,非常有帮助和容易。 (我没有使用hotmail,但它应该也能正常工作)

最佳, STE

答案 4 :(得分:0)

这对我有好处

 import email
 import smtplib

 msg = email.message_from_string('warning')
 msg['From'] = "example@hotmail.fr"
 msg['To'] = "example@hotmail.fr"
 msg['Subject'] = "helOoooOo"

 s = smtplib.SMTP("smtp.live.com",587)
 s.ehlo()
 s.starttls() 
 s.ehlo()
 s.login('example@hotmail.fr', 'pass')


 s.sendmail("example@hotmail.fr", "example@hotmail.fr", msg.as_string())

 s.quit()