1
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.

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

4

5 回答 5

1

问题就在这里:

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

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

您得到的错误是试图说您不能将值分配给字符串中的索引位置。

于 2011-07-25T07:08:45.180 回答
0

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

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

它解释得很好!我过去使用过,非常有用且简单。(我没有使用hotmail,但它应该也可以)

最好的,斯特

于 2011-07-25T07:56:25.993 回答
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()
于 2014-08-09T16:09:14.393 回答
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
于 2011-07-25T07:34:10.193 回答
0

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

有很多例子

于 2011-07-25T07:30:01.203 回答