Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过SMTP Python发送电子邮件时遇到问题_Python_Email_Smtp - Fatal编程技术网

通过SMTP Python发送电子邮件时遇到问题

通过SMTP Python发送电子邮件时遇到问题,python,email,smtp,Python,Email,Smtp,所以我试图用Python通过SMTPlib发送一封电子邮件,但我无法让它工作。我阅读了微软的SMTP规范,并将它们相应地放进去,但我无法让它工作。这是我的密码: # Send an email SERVER = "smtp-mail.outlook.com" PORT = 587 USER = "******@outlook.com" PASS = "myPassWouldBeHere" FROM = USER T

所以我试图用Python通过SMTPlib发送一封电子邮件,但我无法让它工作。我阅读了微软的SMTP规范,并将它们相应地放进去,但我无法让它工作。这是我的密码:

    # Send an email
    SERVER  = "smtp-mail.outlook.com"
    PORT    = 587
    USER    = "******@outlook.com"
    PASS    = "myPassWouldBeHere"
    FROM    = USER
    TO      = ["******@gmail.com"]
    SUBJECT = "Test"
    MESSAGE = "Test"
    message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, MESSAGE)
    try:
        server = smtplib.SMTP()
        server.connect(SERVER, PORT)
        server.starttls()
        server.login(USER,PASS)
        server.sendmail(FROM, TO, message)
        server.quit()
    except Exception as e:
        print e
        print "\nCouldn't connect."
我从键盘记录程序那里得到了密码,但我把它清理了一点。我读过基本的SMTP是如何工作的,但是像
starttls
(方法)这样的东西我不太了解


我真的非常感谢您的帮助。

试试这个。这在Python2.7中起作用

def send_mail(recipient, subject, message):

    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

    username = "sender@outlook.com"
    password = "sender's password"

    msg = MIMEMultipart()
    msg['From'] = username
    msg['To'] = recipient
    msg['Subject'] = subject
    msg.attach(MIMEText(message))

    try:
        print('sending mail to ' + recipient + ' on ' + subject)

        mailServer = smtplib.SMTP('smtp-mail.outlook.com', 587)
        mailServer.ehlo()
        mailServer.starttls()
        mailServer.ehlo()
        mailServer.login(username, password)
        mailServer.sendmail(username, recipient, msg.as_string())
        mailServer.close()

    except error as e:
        print(str(e))


send_mail('recipient@example.com', 'Sent using Python', 'May the force be with you.')

“我不能让它工作”是什么意思?你收到错误信息了吗?它会使你的电脑崩溃吗?如果消息只有一部分,那么将其包装成多部分显然是多余的。