如何从Azure功能应用程序发送电子邮件? [英] How do I send email from an Azure function app?

查看:92
本文介绍了如何从Azure功能应用程序发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经运行了Azure函数App(使用python语言),出于业务需求需要从函数app发送电子邮件.

I have running Azure function App(in python language), for business requirements need to send emails from function app.

为此,我编写了一个python函数

for that, I wrote a python function

from email.message import EmailMessage
import smtplib

def send_mail():
    # message to be sent
    msg = EmailMessage()
    msg.set_content('Test content')

    msg['Subject'] = 'Test'
    msg['From'] = "test@hotmail.com"
    msg['To'] = ["test@hotmail.com"]

    # creates SMTP session 
    s = smtplib.SMTP('smtp-mail.outlook.com', 587) 
    s.ehlo()

    # start TLS for security 
    s.starttls() 
    # Authentication 
    s.login("test@hotmail.com", "password-to-login") 
      
    # sending the mail 
    s.send_message(msg)
    
    # terminating the session 
    s.quit() 

    return

上面的代码块在本地计算机上运行良好.如果我将相同的代码移至Azure Function App,则无法正常工作.

Above block of code working fine in local machine. if I move the same code to Azure Function App it's not working.

我如何使其在Azure函数应用程序上工作?

How do I make it work on the Azure function app?

如何在Azure Function App中从 Gmail 发送电子邮件?

How do I send email from Gmail in Azure Function App?

推荐答案

直接从Azure计算服务中托管的电子邮件服务器将出站电子邮件发送到外部域(例如Outlook.com,gmail.com等)由于公共云服务IP的弹性性质和滥用的可能性,因此不支持该功能.这样,Azure计算IP地址块将添加到公共阻止列表(例如Spamhaus PBL).此政策没有例外.

Sending outbound e-mail to external domains (such as outlook.com, gmail.com, etc) directly from an e-mail server hosted in Azure compute services is not supported due to the elastic nature of public cloud service IPs and the potential for abuse.  As such, the Azure compute IP address blocks are added to public block lists (such as the Spamhaus PBL).  There are no exceptions to this policy.

由于我们不支持从平台上运行服务器和smtp服务器,因此这不会对我们造成影响.

Since we do not support running and smtp server from our platform this should not affect us.

到目前为止,在Azure Web App上使用EMAIL功能的唯一方法是通过SMTP中继.诸如SendGrid之类的第三方服务提供了这类服务.

The only way to use EMAIL functionality as of now on Azure Web App is via an SMTP relay. A third party service such as SendGrid provides these type of services.

在Azure Web Apps体系结构中,实际的Web Apps位于公共前端的后面,这些前端由该数据中心上托管的所有站点共享.

In the Azure Web Apps architecture the actual Web Apps sit behind common Front-Ends which are shared by all the sites hosted on that Data Centre.

该数据中心上托管的站点之一有可能正在发送SPAM电子邮件,并且该IP地址可能会被MAIL服务器列入黑名单.因此,从该地址发送的电子邮件将被邮件服务器拒绝或视为垃圾邮件.在VM或Cloud Services的情况下也存在此限制.Azure使用IP地址池,并且这些地址被重用.这意味着您可以获取一个已经被列入黑名单的IP地址,因为以前有人从该地址发送垃圾邮件,因此您的电子邮件将被邮件服务器拒绝或视为垃圾邮件.

There is a possibility that one of the site hosted on that datacenter is sending SPAM emails and this could have the IP address to be blacklisted by the MAIL Servers. So the e-mails sent from that address will be rejected or considered as SPAM by mail servers.   This limitation exists in case of VM or Cloud Services too. Azure uses a pool of IP Address, and these addresses are reused. That means you could get an IP Address which has already been blacklisted, as someone was sending SPAM from that address before and hence your emails would be rejected or considered as SPAM by mail servers.

这是Cloud中的常见情况,通常建议使用外部邮件服务提供程序(例如SendGrid)进行消息传递.SendGrid相关文章:

This is a common scenario in Cloud and it is typically recommended to use an external Mail Service provider like SendGrid for messaging.   SendGrid related articles: 

如何在Azure中使用SendGrid发送电子邮件: https://azure.microsoft.com/en-in/documentation/articles/sendgrid-dotnet-how-to-send-email/

How to Send Email Using SendGrid with Azure: https://azure.microsoft.com/en-in/documentation/articles/sendgrid-dotnet-how-to-send-email/

这篇关于如何从Azure功能应用程序发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆