python - sendgrid - 如何指定收件人 SMTP 服务器?

标签 python azure smtp sendgrid smtplib

我正在尝试将电子邮件发送到私有(private) SMTP 服务器,并且可以使用 python smtplib 执行此操作,如下所示

s = smtplib.SMTP(server, port)
s.sendmail(me, you, msg.as_string())

尝试将其转换为 Sendgrid(因为 Azure 阻止 SMTP),但它没有指定专用 SMTP 服务器的选项。

import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="225647515662475a434f524e470c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>")
to_email = Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81f5e4f2f5c1e4f9e0ecf1ede4afe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

任何帮助将不胜感激。

谢谢

最佳答案

这很正常,sendgrid 的 python 包不使用 smtp 协议(protocol),但实际上请求一个使用经典 HTTPS 请求的 API...

因此,除非您的 smtp 服务器可以通过 API(定制的?)访问,否则您不可能使用 sendgrid 包。

如果您想自定义 sendgrid 的 python 包,以便指向您自己的 API 端点(该端点将充当您的 smtp 请求的中继),您可以自由修改其源代码:https://github.com/sendgrid/sendgrid-python

关于python - sendgrid - 如何指定收件人 SMTP 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46330958/

相关文章:

Python如何将关系数据放入mongodb+gridfs

c# - 将现有 blob 使用到媒体服务 Assets (API v3)

c# - 如何使用 'Ac­tor Remin­der' 机制在 Service Fabric 中调度任务?

azure - 如何使用 Azure Active Directory B2C 在 React Native 中进行身份验证?

email - 如何在ssmtp中更改发件人姓名?

c# - smtp异常 "failure sending mail"

python - balance() 方法在 python 中调用时不起作用

python - django 中的高效分页和数据库查询

python - torch.flatten() 和 nn.Flatten() 之间的区别

smtp - 通过 SES SMTP 的 Redmine 电子邮件