从Azure上托管的Web应用程序发送gmail [英] Send gmail from web application hosted on Azure

查看:131
本文介绍了从Azure上托管的Web应用程序发送gmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手到Azure,但将我的应用程序成功发布到云端以及我需要的数据库后端(SQL Server)。



除了电子邮件功能,应用程序内的所有内容都按照预期的方式工作。 200新X-45旗新新新旗新新旗新新旗新新旗新新旗新新旗新200旗新新旗新新旗旗新新旗新新旗旗新新旗新旗旗新新旗旗新集200人200 200 X-该应用程序不会崩溃,并且尝试配置远程调试已被证明是困难的。



这是我用于发送我的电子邮件的代码段:

  //解析出版电子邮件中显示的html文档
StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings [ 模板]));
string body = reader.ReadToEnd();

//填充消息变量的占位符
body = body.Replace(<%holder%>,value);
...为了简洁省略

尝试
{
var smtp = new SmtpClient
{
Host =smtp.gmail.com ,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address,fromPassword )
};

使用(var message = new MailMessage(fromAddress,toAddress)
{
IsBodyHtml = true,
Subject = subject,
Body = body,$新新新新旗新新新新旗新新旗新新旗新新旗200新新新新旗新新旗新新旗200新新新新旗新新旗200新新新新旗新新旗旗号
}
}

应用没有崩溃,所以没有错误信息离开。所以我想也许没法抓住这个文件:

  StreamReader(HttpContext.Current.Server.MapPath (ConfigurationManager.AppSettings [ 模板])); 
string body = reader.ReadToEnd();

所以我试着发送一个硬编码的字符串作为测试:


$新新旗新新新旗新新新旗新新旗新新旗新新旗新新旗200新新新新旗新新款:

仍然没有收到电子邮件,没有错误消息。 200新新新旗新新新旗新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新旗新旗旗新旗旗Azure允许通过第三方电子邮件客户端发送电子邮件? Fyi - 以上代码针对本地主机。

解决方案

从公共云发送电子邮件并不像一些人认为的那样微不足道。新新200的X- 200 X-特别是当您打算使用公共邮件服务时。



我的第一个猜测是,Azure数据中心的IP地址(或您托管的地址)可能被邮件服务器(包括Microsoft自己的Office 365)列入黑名单。 / p>

我还要提到,从Azure中托管的应用程序发送电子邮件的推荐方法是使用 SendGrid 。他们有一个自由的层次。更多信息从Microsoft就此主题,可以在这里找到



解决问题的确切原因的唯一方法是联系GMail支持,并询问他们是否以任何方式阻止网络从Azure云连接的客户端。或者在您的Web应用程序所在的同一数据中心中创建虚拟机,安装一些免费/试用邮件客户端,使用Google Mail进行配置并尝试发送电子邮件。 X- 20045454545 X- 20045 X- 20045 X- 20045 X- 20045 CEEC X-

Newbie to Azure, but got my application successfully published to the cloud as well as my needed database backends (SQL server).

Everything within the application is working as expected except email functionality. I am sending out email alerts via a gmail account, but they do not appear to get sent out. The application does not crash and trying to configure remote debugging has proven difficult.

This is the snippet I am using to send out my emails:

        //Parse html document which will show in outgoing email
        StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["template"]));
        string body = reader.ReadToEnd();

        //Populate placeholders with message variables
        body = body.Replace("<%holder%>", value);
        ...Omitted for brevity

        try
        {
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };

            using (var message = new MailMessage(fromAddress, toAddress)
            {
                IsBodyHtml = true,
                Subject = subject,
                Body = body,
            })
            {
                smtp.Send(message);
            }
        }

Application didn't crash, so no error message to go off of. So I thought maybe it wasn't able to grab the file in the lines:

StreamReader(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["template"]));
string body = reader.ReadToEnd();

So I tried just sending a hardcoded string as a test:

string body = "test";

Still no email received and no error message to go off of. I'm new to azure web hosting, but is there some configuration I could be missing here? Does Azure allow sending email through third party email clients? Fyi - the above code works against localhost.

解决方案

Sending e-mail from a public cloud is not as trivial as some people believe. There are a lot of things to figure out in order to not get blacklisted. Especially when you intend to use a public mail service.

My first guess is that Azure Data Center IP addresses (or the one you are hosted on) might be blacklisted by mail servers (including Microsoft's very own Office 365).

I have to also mention that recommended way for sending e-mail from an Application hosted in Azure is by using SendGrid. They have a free tier. More information from Microsoft on that subject, can be found here.

The only way to troubleshoot the exact cause of your problem is to contact GMail support and ask them if they block in any way network clients connecting from Azure cloud. Or create a VM in the same Data Center where your web application lives, install some free / trial Mail client, configure it with Google Mail and try to send e-mails. The result most probably will be same as with your application.

这篇关于从Azure上托管的Web应用程序发送gmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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