无法使用Python发送邮件

无法使用Python发送邮件,python,smtp,Python,Smtp,我试着通过不同的网站上给出的代码,但每个地方的格式都是相同的。由于属性错误,我无法发送邮件:“str”对象没有属性“as\u string”。这是理所当然的 text=msg.as_string() #-*-编码:utf-8-*- 作为pd进口熊猫 输入io 导入smtplib 从email.mime.text导入MIMEText 从email.mime.multipart导入MIMEMultipart 从email.mime.base导入MIMEBase 从电子邮件导入编码器 将numpy作为

我试着通过不同的网站上给出的代码,但每个地方的格式都是相同的。由于属性错误,我无法发送邮件:“str”对象没有属性“as\u string”。这是理所当然的 text=msg.as_string()

#-*-编码:utf-8-*-
作为pd进口熊猫
输入io
导入smtplib
从email.mime.text导入MIMEText
从email.mime.multipart导入MIMEMultipart
从email.mime.base导入MIMEBase
从电子邮件导入编码器
将numpy作为np导入
msg=MIMEMultipart()
require_cols=[8]
excel\u file=pd.ExcelFile('test\u jan20.xlsx')
打印(excel\u文件)
打印(excel文件、工作表名称)
df=excel\u file.parse('Sheet1',index\u col=8)
打印(df)
电子邮件='阿罗拉。prachi1999@gmail.com'
密码='zxozxxrlifvsmrnl'
发送电子邮件=['prachi]。arora@incedoinc.com","阿迪蒂"。dhooria@incedoinc.com']
主题='这是一封测试邮件'
msg=“早上好!”
msgHTML=“”
这是一封使用python的测试邮件
问候

普拉杰 """ 打印('已选中') #连接并登录到电子邮件服务器 server=smtplib.SMTP('SMTP.gmail.com',587) server.starttls() 服务器登录(电子邮件、密码) text=msg.as_string() 打印(文本) #循环发送到每个电子邮件 对于发送电子邮件中的发送电子邮件: #为每个电子邮件地址设置MiMemMultipart(如果我们不这样做,电子邮件将在发送的每个电子邮件上连接) msg=MIMEMultipart() msg['From']=电子邮件 msg['To']=发送电子邮件给 msg['Subject']=主语 #将消息附加到MIMEMultipart对象 msg.attach(MIMEText(消息'plain')) 附加(MIMEText(messageHTML,'html')) #将电子邮件发送到此特定电子邮件地址 server.sendmail(email,send\u to\u email,message.as\u string()) 打印('已发送邮件')

我认为问题在于,在定义msg=MIMEMultipart()之后,您正在用“Morning!”重新定义
msg
变量,这导致了问题

将msg=“Morning!”更改为类似message=“Morning!”
希望它能工作

我们不能不看到代码…我刚刚添加了代码,由于一些技术问题,信息是在没有代码的情况下上载的。在调用
msg.as\u string()
,msg是普通字符串
“Morning!”
,字符串没有
。as\u string()
方法。(稍后您将重新定义
msg=MIMEMultipart()
,并且该对象类型确实具有该方法,但在此之前您正在调用
msg.as\u string()
# -*- coding: utf-8 -*-
import pandas as pd
import io
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import numpy as np

msg=MIMEMultipart()

require_cols = [8]
excel_file = pd.ExcelFile('test_jan20.xlsx')

print(excel_file ) 
print(excel_file.sheet_names)

df = excel_file.parse('Sheet1', index_col=8) 
print(df)


email = 'arora.prachi1999@gmail.com'
password = 'zxozxxrlifvsmrnl'
send_to_emails = ['prachi.arora@incedoinc.com', 'aditi.dhooria@incedoinc.com']
subject = 'This is a test mail'

msg= "Morning!"
msgHTML ="""
<html>
  <head></head>
  <body>
  This is a test mail using python<br>
  Regards<br><br>
  Prachi
  </body>
</html>
"""
print('checked')

# Connect and login to the email server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
print(text)

# Loop over each email to send to
for send_to_email in send_to_emails:
    # Setup MIMEMultipart for each email address (if we don't do this, the emails will concatenate on each email sent)
    msg = MIMEMultipart()
    msg['From'] = email
    msg['To'] = send_to_email
    msg['Subject'] = subject

# Attach the message to the MIMEMultipart object
msg.attach(MIMEText(message, 'plain'))
msg.attach(MIMEText(messageHTML, 'html'))

# Send the email to this specific email address
server.sendmail(email, send_to_email, message.as_string()) 
print('mail sent')