Silverlight中调用WebService-发送邮件测试实例

Silverlight中调用WebService-发送邮件测试实例

Silverlight 能够构建强大的企业级应用程序,关键和Silverlight2.0强大数据通信 是分不开的,包括SOAP服务的访问,RESTful服务的访问,以及基 于Http协议和Socket通信等。在Silverlight中一些基本的功能都适用于Http和Https通信。

其中主要特点:

     A:始终允许同域调用

     B:如果正确配置负载有Web服务的Web服务器,可以跨域访问

     C:仅仅支持Get和Post谓词

     D:所有请求都是异步的-这也是SL用户体验最为吸引人一个特点

     E:只有"00确定"和"404为找到"状态可用

我们看到B支持跨域访问,Silverlight默认情况是请求的URL进行严格限制的,只允许访问同一子目录下的URL资源。禁止跨域访问的,例如当前我们在本地的Web程序中引用新浪网提供天气预报服务http://sina.com/service.asmx ,默认情况下会抛出一个异常.

这个异常抛出的主要原因是在完成事件Complete引用的参数为空,解决方案:在需要被访问的站点根目录下添加两个策略文件-两个XML文件 clientaccesspolicy.xml,crossdomain.xml即可,注意两个XML文件最好使用默认的UTF-8编码格式,如果修改在 程序中极易出现错误,且不容易发现.主要是因为请求都是异步的发送客户端的数据格式XML编码就是UTF-8格式。

这是昨天遇到一个问题,下面做一个简单的在Silverlight程序中调用本地WebService(非跨域)实例.

(1)创建WebService并实现WebService:-


<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1         // WebService访问数据
 2        [WebMethod]
 3          public  List < BookEnticy >  GetAllBookEnticy()
 4          {
 5             List < BookEnticy >  getBooklist  =   new  List < BookEnticy >   {
 6               new  BookEnticy {BookId = 120 ,BookName = " 我的非正常生活! " } ,
 7               new  BookEnticy {BookId = 121 ,BookName = " 中央警备司令部 " } ,
 8               new  BookEnticy {BookId = 122 ,BookName = " 警戒线 " }
 9
10             }
;
11              // 导入System.Data.Linq 空间
12             IDbConnection getcon  =   new  SqlConnection            ( " server=local;database=TestLinq;uid=sa;pwd=sa " );
13             DataContext getcontent  =   new  DataContext(getcon);
14
15             GetServerClintDataWS newdata  =   new  GetServerClintDataWS();
16             List < string >  getlist = from getlistinfo  in  newdata.GetAllBookEnticy()
17                                          select getlistinfo.BookName;
18              return  getBooklist;
19         }

(2)Silverlight中引用并调用

选中SL项目右击弹出的菜单选择  添加服务引用,SL应用程序中会自动生成一个文件夹

当添加引用后,自动添加SL程序中对应该服务的本地代理类 ,代理类是基于System.ServiceModel空间种ClintBase构建的,另外一个配置文件 ServiceReferences.ClintConfig是客户端配置文件,用来配置访问服务地址,绑定契约等信息

Silverlight只支持异步调用,调用完成奖触发一个Complete事件,最终将在OnComplete事件中进行数据绑定

根据上面步骤用Silverlight简单发邮件功能:

A:创建WebService并实现:核心代码如下


<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->       [WebMethod]
        
/**/ /// 发送Email
        
/// 参数:fromaddress-发件人   toaddress-收件人  标题-title  content-内容

         public   bool  SendEmail( string  fromaddress,  string  toaddress,  string  title,  string  content)
        
{
            
try
            
{
                MailMessage newmail 
=   new  MailMessage();
                newmail.From 
=   new  MailAddress(fromaddress);
                newmail.To.Add(
new  MailAddress(toaddress)); // 因为可以抄送 对应多人
                newmail.Subject  =  title;
                newmail.Body 
=  content;
                newmail.IsBodyHtml 
=   false ;

                
// use SmtpClint send Email
                SmtpClient getclint  =   new  SmtpClient();
                getclint.EnableSsl 
=   true ;
                getclint.Send(newmail);

                
return   true ;
            }

            
catch  (Exception se)
            
{
                
return   false ;
            }

            
return   false ;
        }

在Asp.net实现发送邮件功能必须引用System.net.Mail空间,发送邮件细节请参考MSDN:http://msdn.microsoft.com/zh-cn/library/system.net.mail.mailmessage.aspx ,WebService 中发送邮件方法总共接受四个参数:FromAddress-发件人  ToAddress-收件人  Title:标题 Body:内容,在建立WebService发送邮件自前需要建立一个邮件服务器,一般使用Gmail可以使用自己的账号,在根目录Web.Config 中配置,详细代码如下:

<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> <!-- 配置邮件服务器 发送Emai use Gmail -->
    
< system.net >
        
< mailSettings >
            
< smtp >
                
< network host = " smtp.gmail.com "  port = " 587 "  userName = " [email protected] "  password = ""   />
            
</ smtp >
        
</ mailSettings >
    
</ system.net >

配置完必须在Silverlight中添加服务引用

(2)Silverlight做前台页面:

效果图-(简化版):

页面Silverlight代码:


<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->  < Grid  x:Name ="LayoutRoot"  Background ="Bisque" >
        
< Canvas >
            
< Grid >
                
< Grid.RowDefinitions >
                    
< RowDefinition  Height ="70" ></ RowDefinition >
                    
< RowDefinition  Height ="70" ></ RowDefinition >
                    
< RowDefinition  Height ="80" ></ RowDefinition >
                    
< RowDefinition  Height ="*" ></ RowDefinition >
                
</ Grid.RowDefinitions >
                
                
< Grid.ColumnDefinitions >
                    
< ColumnDefinition  Width ="80" ></ ColumnDefinition >
                    
< ColumnDefinition  Width ="*" ></ ColumnDefinition >
                
</ Grid.ColumnDefinitions >
                
< TextBlock  Text ="收件人:"  Grid.Row ="0"  Grid.Column ="0"  TextAlignment ="Center"  HorizontalAlignment ="Center"  VerticalAlignment ="Center"  FontSize ="14" ></ TextBlock >
                
< TextBox  x:Name ="address"  Grid.Row ="0"  Grid.Column ="1"  Height ="40"  Width ="250"  FontSize ="14"  TextAlignment ="Left"   HorizontalAlignment ="Center"  VerticalAlignment ="Center" >   </ TextBox >
                
< TextBlock  Text ="标  题:"  Grid.Row ="1"  Grid.Column ="0"   FontSize ="14"  TextAlignment ="Center"   HorizontalAlignment ="Center"  VerticalAlignment ="Center" ></ TextBlock >
                
< TextBox  x:Name ="title"  Grid.Row ="1"  Grid.Column ="1"  Height ="40"  Width ="250"  FontSize ="14"  TextAlignment ="Left"  HorizontalAlignment ="Center"  VerticalAlignment ="Center" ></ TextBox >
                
< TextBlock  Text ="内  容:"  Grid.Row ="2"  Grid.Column ="0"   FontSize ="14"   HorizontalAlignment ="Center"  VerticalAlignment ="Center" ></ TextBlock >
                
< TextBox  TextWrapping ="Wrap"  x:Name ="content"  Grid.Row ="2"  Grid.Column ="1" ></ TextBox >
                
< Button  Content ="Send"  Grid.Row ="3"  Grid.Column ="0"   Height ="45"   Click ="Button_Click" ></ Button >
            
</ Grid >
        
</ Canvas >

    
</ Grid >

当点击Send触发事件后台处理代码:


<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->   // send Email
         private   void  Button_Click( object  sender, RoutedEventArgs e)
        {
            
string  fromaddress  =   " [email protected] " ;
            ServiceReference2.SendEmailTestSoapClient getclint 
=   new  TestWebServiceSL.ServiceReference2.SendEmailTestSoapClient();

            getclint.SendEmailCompleted 
+=   new  EventHandler < TestWebServiceSL.ServiceReference2.SendEmailCompletedEventArgs > (getclint_SendEmailCompleted);
            
        }
        
        
void  getclint_SendEmailCompleted( object  sender, TestWebServiceSL.ServiceReference2.SendEmailCompletedEventArgs e)
        {
            
// 判断并提示
             if  (e.Error  !=   null )
            {
                HtmlPage.Window.Alert(
" Send  Suceess! " );
            }
            
else
            {
                HtmlPage.Window.Alert(
" Send Faile! " );
            }   
        }

 

http://en.wiktionary.org/wiki/Special:Search?search=Silverlight%E4%B8%AD%E8%B0%83%E7%94%A8WebService-%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%AE%9E%E4%BE%8B%0D%0A%0D%0ASilverlight%E8%83%BD%E5%A4%9F%E6%9E%84%E5%BB%BA%E5%BC%BA%E5%A4%A7%E7%9A%84%E4%BC%81%E4%B8%9A%E7%BA%A7%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%EF%BC%8C%E5%85%B3%E9%94%AE%E5%92%8CSilverlight2.0%E5%BC%BA%E5%A4%A7%E6%95%B0%E6%8D%AE%E9%80%9A%E4%BF%A1%E6%98%AF%E5%88%86%E4%B8%8D%E5%BC%80%E7%9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4%B8%BA%E5%90%B8%E

你可能感兴趣的:(应用服务器,webservice,asp.net,Gmail,silverlight)