我一直在尝试(并且失败)弄清楚如何通过Python发送电子邮件.
试试这里的例子:http: //docs.python.org/library/smtplib.html#smtplib.SMTP
但server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
在我没有SSL连接的反弹后添加了这条线.
现在我明白了:
Traceback (most recent call last):
File "C:/Python26/08_emailconnects/12_29_EmailSendExample_NotWorkingYet.py", line 37, in <module>
server = smtplib.SMTP('smtp.gmail.com', 65)
File "C:\Python26\lib\smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python26\lib\smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python26\lib\smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python26\lib\socket.py", line 512, in create_connection
raise error, msg
error: [Errno 10060] A connection attempt failed because the connected party did …
Run Code Online (Sandbox Code Playgroud) 我想编写一个使用Python的smtplib发送电子邮件的程序.我搜索了文档和RFC,但找不到任何与附件相关的内容.因此,我确信我错过了一些更高级别的概念.有人能告诉我附件如何在SMTP中工作吗?
我有以下脚本用于使用python发送邮件
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
FROMADDR = "myaddr@server.com"
PASSWORD = 'foo'
TOADDR = ['toaddr1@server.com', 'toaddr2@server.com']
CCADDR = ['ccaddr1@server.com', 'ccaddr2@server.com']
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Test'
msg['From'] = FROMADDR
msg['To'] = ', '.join(TOADDR)
msg['Cc'] = ', '.join(CCADDR)
# Create the body of the message (an HTML version).
text = """Hi this is the body
"""
# Record the MIME types of both …
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用 python 发送电子邮件。我读过的所有网络教程都解释了如何使用 Gmail 进行操作。
但是,从 2022 年 5 月 30 日起(尽管每个人都可以自由地使用自己的帐户做任何他想做的事情),Google 制定了一项新政策,规定:
为了确保您的帐户安全,从 2022 年 5 月 30 日开始,Google 将不再支持使用仅要求您提供用户名和密码的第三方应用或设备。登录您的 Google 帐户。
来源: https: //support.google.com/accounts/answer/6010255
所以我的问题是有没有其他方法可以使用 python 发送电子邮件(包括属于其他公司的电子邮件帐户)?
这是我发送电子邮件的功能:
def send_email_fct(filename, filepath, fromaddr, mdpfrom, toaddr):
"""" filename: file name to be sent with extension
filepath: file path of the file to be sent
fromaddr: sender email address
mdpfrom: password of sender email address
toaddr: receiver email address"""
msg = MIMEMultipart() # instance of MIMEMultipart
msg['From'] = fromaddr
msg['To'] …
Run Code Online (Sandbox Code Playgroud) 在没有很多MIME知识的情况下,我试图学习如何编写Python脚本来发送带有文件附件的电子邮件.在交叉引用Python文档,Stack Overflow问题和一般Web搜索之后,我使用以下代码[1]进行了测试,并对其进行了测试.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
fromaddr = "YOUR EMAIL"
toaddr = "EMAIL ADDRESS YOU SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE EMAIL"
body = "TEXT YOU WANT TO SEND"
msg.attach(MIMEText(body, 'plain'))
filename = "NAME OF THE FILE WITH ITS EXTENSION"
attachment = open("PATH OF THE FILE", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个带有身份验证的简单smtp-sender.这是我的代码
SMTPserver, sender, destination = 'smtp.googlemail.com', 'user@gmail.com', ['reciever@gmail.com']
USERNAME, PASSWORD = "user", "password"
# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'
content="""
Hello, world!
"""
subject="Message Subject"
from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
from email.MIMEText import MIMEText
try:
msg = MIMEText(content, text_subtype)
msg['Subject']= subject
msg['From'] = sender # some …
Run Code Online (Sandbox Code Playgroud) 您好我有unicode电子邮件的问题,当我尝试发送西班牙语单词时:"Añadir"或其他系统崩溃,我尝试在此链接上说的内容:Python 3 smtplib发送unicode字符而不是工作.
这是我的错误代码:
server.sendmail(frm, to, msg.as_string())
g.flatten(self, unixfrom=unixfrom)
self._write(msg)
self._write_headers(msg)
header_name=h)
self.append(s, charset, errors)
input_bytes = s.encode(input_charset, errors)
Run Code Online (Sandbox Code Playgroud)
UnicodeEncodeError:'ascii'编解码器无法编码位置7中的字符'\ xf1':序数不在范围内(128)
这是服务器上的代码:
msg = MIMEMultipart('alternative')
frm = "sales@bmsuite.com"
msg['FROM'] = frm
to = "info@bmsuite.com"
msg['To'] = to
msg['Subject'] = "Favor añadir esta empresa a la lista"
_attach = MIMEText("""Nombre:Prueba; Dirección:Calle A #12.""".encode('utf-8'), _charset='utf-8')
msg.attach(_attach)
server.sendmail(frm, to, msg.as_string())
server.quit()
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我正在给自己写一个简单的sendmail函数,我一直收到这个错误:
NameError:未定义名称"SMTPException"
我的代码出了什么问题?有什么建议?
import smtplib
sender = "user@gmail.com"
receiver = ["user@gmail.com"]
message = "Hello!"
try:
session = smptlib.SMTP('smtp.gmail.com',587)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender,'password')
session.sendmail(sender,receiver,message)
session.quit()
except SMTPException:
print('Error')
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python脚本向自己发送电子邮件,幸运的是我遇到了这篇文章:
麻烦的是,smtplib以纯文本形式发送脚本的密码,我对它的安全性持怀疑态度.此外,我的脚本以纯文本格式包含我的用户名和密码.有没有什么好方法可以使用Python并发送电子邮件而无需将我的密码保留为纯文本?
我也在StackOverflow上看到了这一点: Python smtplib安全性, 但答案并不能完全帮助我解决这个冲突.但是,我还没准备好放弃.
更多信息:我正在尝试将我的Raspberry Pi设置为通过网站进行擦除的服务器.当关于网站的具体事情发生变化时,我希望通过电子邮件收到通知.但是,我不想让我的Pi坐在一个带有我的用户名和密码的纯文本脚本.
所以我正在尝试在python中编写一个脚本,该脚本登录到我的gmail帐户,然后很快在GUI中告诉我该消息是什么.我稍后会对代码做更多的事情,使它有点程序更有用但是现在我只是能够解析我得到的原始信息.这是我的代码:
#Read Email Script
import imaplib
import email
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('username@gmail.com', 'passwordgoeshere')
mail.list()
mail.select("INBOX") # connect to inbox.
result, data = mail.search(None, "ALL")
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
result, data = mail.fetch(latest_email_id, '(RFC822)')
raw_email = data[0][1]
email_message = email.message_from_string(raw_email)
print (email_message['Subject'])
Run Code Online (Sandbox Code Playgroud)
现在基本上应该尝试读出发送到我收件箱的最新电子邮件的主题.但是,我在控制台中收到以下错误消息:
>>>
Traceback (most recent call last):
File "C:/Users/Dhruvin Desai/Documents/Python/script.py", line 21, in <module>
email_message = email.message_from_string(raw_email)
File "C:\Python33\lib\email\__init__.py", line 40, in message_from_string
return Parser(*args, **kws).parsestr(s)
File "C:\Python33\lib\email\parser.py", line 69, in parsestr
return self.parse(StringIO(text), …
Run Code Online (Sandbox Code Playgroud) smtplib ×10
python ×9
email ×6
gmail ×4
smtp ×4
python-3.x ×2
attachment ×1
mime ×1
parsing ×1
raspberry-pi ×1
unicode ×1