通过python发送邮件

时间:2014-07-10 12:06:30

标签: python email outlook

我一直在尝试从python向Outlook发送邮件。身体没有出现在邮件中。邮件将与主题一起发送。身体是空白的。可能是什么问题?

这是我的代码:

import smtplib

    username = "neooooo@example.com"
    password = "@death123"

    print("Logged in ")

    vtext = "nihjdoiwjadv@example.com"
    message = "this is the message to be sent"

    msg = """From: %s
    To: %s
    Subject: Hi
    Body:%s""" % (username, vtext, message)

    print("Connecting to server")

    server = smtplib.SMTP('smtp.office365.com',587)
    server.starttls()
    server.login(username,password)
    server.sendmail(username, vtext, msg)
    server.quit()

    print("Done")

2 个答案:

答案 0 :(得分:0)

正文不是标题的一部分,尤其是没有名为 Body 的标题。邮件的正文位于标题之后,用空行隔开。

答案 1 :(得分:0)

试试这段代码。它对我有用。

import  smtplib,getpass,os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

os.system('clear')
msg=MIMEMultipart()
print "---------Python Mail Sender--------\n---------ONLY GMAIL Sender---------"
frm=raw_input("From :  ")
to=raw_input("To : ")
msg['From']=frm
msg['To']=to
msg['Subject']=raw_input("Enter Subnject of mail : ")
text=raw_input("Enter text to send in mail : ")

msg.attach(MIMEText(text))
try :
    mailserver=smtplib.SMTP("smtp.gmail.com",587)
    mailserver.ehlo()
    mailserver.starttls()
    mailserver.ehlo()
    mailserver.login(frm,getpass.getpass("Enter you password(Will not be visible as you Enter) : "))

    mailserver.sendmail(frm,to,msg.as_string())
except Exception,e:
    print "ERROR : ",e

finally:
    mailserver.quit()