Azure Lei Zhang的博客

weibo: LeiZhang的微博/QQ: 185165016/QQ群:319036205/邮箱:leizhang1984@outlook.com/TeL:139-161-22926

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  Windows Azure Platform 系列文章目录

 

  现在我介绍使用Windows Azure发送邮件的第二种方法。在第一部分里,我介绍了如何使用内部的邮件转发服务器(Email Forwarder Service)替代Windows Azure上的应用,来实现发送邮件的功能。这章我将介绍如何直接在Windows Azure上使用邮件服务的API.

 

使用邮件服务的API

  这种模式使用Exchange Server 2007或者Exchange Server 2012的Web服务,直接从Windows Azure向外发送邮件。这种方法也可以使用其他的邮件服务器提供的Web服务接口,但是这篇文章只介绍了在Microsoft Exchange Server下的实现。

  因为Web Role和Worker Role都可以通过HTTP或HTTPS连接Internet上的资源,所以Web / Worker Role也可以访问在Exchange Server上的Web服务。Exchange Server 2007和Exchange Server 2010都支持EWS(Exchange Web Service),这是一种自动化接口。Exchange Web Services提供了丰富的功能,它支持最常见的邮件自动化服务,比如发送邮件,添加附件,检查用户的邮件,配置代理访问等等。如果您想参考EWS提供功能的完整列表,请参考MSDN

  如果您公司倾向于使用Exchange Online而不是内部的Exchange邮件服务器,因为Exchange Onle Starndard和Exchange Online Dedicated都提供相同的EWS的接口,所以在Windows Azure上不需要修改任何代码。Exchange Onbline是Microsoft Business Productivity Online Suite (BPOS)的一部分,BPOS是一套由微软提供的消息和协作解决方案(a set of messaging and collaboration solutions)。

  下图显示了Windows Azure如何利用EWS发送电子邮件。

 

Sample Code

  EWS Managed (托管)API提供了强类型的.NET接口,可以与Exchange Web Service交互。一旦安装,Exchange Web Services Managed API,您首先要做的是,打开Visual Studio,在您现有的Windows Azure 项目里,添加对Microsoft.Exchange.WebServices.dll的引用。

 

在测试服务器上使用EWS Managed API

在测试服务器上部署自签名的证书,这在Microsoft .NET Framework上是不被信任的。如果您在测试服务器上使用EWS Managed API,您可能会收到如下的错误:

   为了解决这个问题,你可以按照以下的代码来禁用签名问题。你可以使用 #if DEBUG或者类似的技术,以确保此代码不用于生产环境。

using System.Net.Security;
 using System.Security.Cryptography.X509Certificates;
 
// Hook up the cert callback.
 System.Net.ServicePointManager.ServerCertificateValidationCallback =
       delegate(
             Object obj,
             X509Certificate certificate,
             X509Chain chain,
             SslPolicyErrors errors)
       {
             // Validate the certificate and return true or false as appropriate.
             // Note that it not a good practice to always return true because not
             // all certificates should be trusted.
       };

 

创建ExchangeService对象实例

接下来就是创建和配置一个ExchangeService实例,来连接服务器。注意使用Autodiscover,基于一个email地址来确定服务器的网址。

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
 
//Retrieve the credential settings from the Azure configuration
 
string userName = RoleEnvironment.GetConfigurationSettingValue("EWSUserName");
 
string password = RoleEnvironment.GetConfigurationSettingValue("EWSPassword");
 
string domain = RoleEnvironment.GetConfigurationSettingValue("EWSDomain");
 
service.Credentials = new WebCredentials(userName, password, domain);
 
// In case the EWS URL is not known, then the EWS URL can also be derived automatically by the EWS Managed API 

// using the email address of a mailbox hosted on the Exchange server
 
string emailAddress = RoleEnvironment.GetConfigurationSettingValue("emailAddress");
 
service.AutodiscoverUrl(emailAddress); 

 

发送邮件

一旦ExchangeService对象实例被创建和初始化,它就可以被用作发送和接收邮件。

EmailMessage message = new EmailMessage(service);
 
message.ToRecipients.Add("someone@server.com");
 
message.From = new EmailAddress("someone@azureapp.com");
 
message.Subject = "Sending mail from Windows Azure";
 
message.Body = new MessageBody(BodyType.HTML, "This is the body of the mail sent out from Windows Azure environment");
 
//code for sending attachments.
 
//The AddFileAttachment method takes in a 'display name' and 'byte array' as parameters
 
message.Attachments.AddFileAttachment("Attachment1", attachment1);
 
//The following property 'ContentId' is used in case the attachment needs to be referenced from the body of the mail
 
message.Attachments[0].ContentId = "Attachment1";
 
// The following method sends the mail and also saves a copy in the 'Sent Items' folder
 
message.SendAndSaveCopy();

 

架构考虑

对于任何解决方案来说,了解架构是非常重要的。在Email Server Web Service APIs里面,我们要考虑:

  • 成本:从Windows Azure出站到EWS的数据流将会增加额外的费用。总的费用由每一个单独的解决方案中电子邮件数量和带宽使用来决定,必须在实现该模式之前进行考虑。
  • 性能:附件如果很大的情况下,可能会带来一些性能的影响。因为附件需要被Exchange Server(局域网内或者Internet上)上的EWS序列化和下载。这点也必须仔细考虑。

 

参考资料:http://blogs.msdn.com/b/windowsazure/archive/2010/10/15/adoption-program-insights-sending-emails-from-windows-azure-part-2-of-2.aspx

posted on 2012-06-26 13:49  Lei Zhang的博客  阅读(1790)  评论(0编辑  收藏  举报