Windows Azure Cloud Service (28) 在Windows Azure发送邮件(中)

简介:

 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


本文转自Lei Zhang的博客博客园博客,原文链接:http://www.cnblogs.com/threestone/archive/2012/06/26/2563298.html,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
Java Unix 应用服务中间件
使用java service wrapper把windows flume做成服务
使用java service wrapper把windows flume做成服务
|
7月前
|
关系型数据库 MySQL Windows
windows安装mysql报错Remove of the Service Denied!
windows安装mysql报错Remove of the Service Denied!
|
10月前
|
C# Windows
震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……(上)
震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……
|
10月前
|
调度 C# Windows
震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……(下)
震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……(下)
|
数据采集 安全 Windows
解决关于Windows Defender Antivirus Service自启造成运行python程序时,Windows的cpu和内存占用过高问题
启用“关闭Windwos defender”服务解决阿里云Windows服务器的卡顿问题,并列举了网上一些错误的解决方法。
10024 3
解决关于Windows Defender Antivirus Service自启造成运行python程序时,Windows的cpu和内存占用过高问题
|
7月前
|
Windows
Windows 11 蓝屏 Stop code - SYSTEM SERVICE EXCEPTION What failed - igdkmd64.sys
Windows 11 蓝屏 Stop code - SYSTEM SERVICE EXCEPTION What failed - igdkmd64.sys
106 0
|
12月前
|
Windows
Windows 11 蓝屏 Stop code - SYSTEM SERVICE EXCEPTION What failed - igdkmd64.sys
Windows 11 蓝屏 Stop code - SYSTEM SERVICE EXCEPTION What failed - igdkmd64.sys
248 0
|
Windows
windows server 2012 R2 .NET Runtime Optimization Service CPU占用高
windows server 2012 R2 .NET Runtime Optimization Service CPU占用高
354 0
|
人工智能 安全 数据处理
阿里云的WINDOWS service RS 12的使用体验
我,胡颖,来自湖南文理学院国际学院信息管理与信息系统21102班。信息管理与信息系统是一门普通高等学校本科专业,属管理科学与工程类专业。该专业对于学生的要求是接受系统和设计方法以及信息管理方法的基本训练,具备综合运用所学知识分析和解决问题的基本能力。阿里云是全球领先的云计算及人工智能科技平台,致力于以在线公共服务的方式,提供安全、可靠的计算和数据处理能力,让计算和人工智能成为普惠科技。基于培养自己的计算机专业的要求,我决定尝试一下体验阿里云的功能。
|
程序员 C# Windows
Windows Service 小品
Windows Service 小品
117 0
Windows Service 小品

热门文章

最新文章