我正在从Windows文件夹中选择一个文件,并试图通过gmail发送。
这段代码在单独的python程序中执行时运行良好,但我无法在类中使用同一个程序
import smtplib
import base64
filename = str("JDI Offline.txt").split('\\')[-1:][0]
filepath = "C:\Windows\JDI Offline.txt"
fo = open(filepath, "rb")
filecontent = fo.read()
fo.close()
encodedcontent = base64.b64encode(filecontent)
sender = 'vusvarshil@gmail.com'
receiver = 'vusvarshil@gmail.com'
marker = "AUNIQUEMARKER"
body = """
Test to send attatchment
"""
part1 = """From: From Varshil<vusvarshil@gmail.com>
To:me
Subject: Sending attatchment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit
%s
--%s
""" % (body, marker)
part3 = """Content-Type:multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attatchment; filepath=%s
%s
--%s--
""" % (filename, filepath, encodedcontent, marker)
message = part1 + part2 + part3
try:
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('vusvarshil@gmail.com', 'xxxxxxxx')
mail.sendmail('vusvarshil@gmail.com', 'vusvarshil@gmail.com', message)
print "Mail Sent successfully"
except Exception:
print "Error"
当我在课堂上使用这段代码时,我收到一封空白邮件。我附上了截图
Program of program when used inside a class
Program of program when used without a class
类sendmail: def send\u mail(): 导入smtplib 导入base64
filename = str("JDI Offline.txt").split('\\')[-1:][0]
filepath = "C:\Windows\JDI Offline.txt"
fo = open(filepath, "rb")
filecontent = fo.read()
fo.close()
encodedcontent = base64.b64encode(filecontent)
sender = 'vusvarshil@gmail.com'
receiver = 'vusvarshil@gmail.com'
marker = "AUNIQUEMARKER"
body = """
Test to send attatchment
"""
part1 = """From: From Varshil<vusvarshil@gmail.com>
To:me
Subject: Sending attatchment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit
%s
--%s
""" % (body, marker)
part3 = """Content-Type:multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attatchment; filepath=%s
%s
--%s--
""" % (filename, filepath, encodedcontent, marker)
message = part1 + part2 + part3
try:
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('vusvarshil@gmail.com', 'xxxxxxxx')
mail.sendmail('vusvarshil@gmail.com', 'vusvarshil@gmail.com', message)
print "Mail Sent successfully"
except Exception:
print "Error"
ob=sendmail()
ob.send_email()
有人请告诉我出了什么问题
我找到了解决问题的办法。这个代码没有工作,所以我搜索了一个新的代码。我张贴在这里的代码供将来参考。 在下面的代码中,我从文件中读取数据并附加标签,因为这是我的要求。您可以根据需要更改代码
相关问题 更多 >
编程相关推荐