更新时间:2022-09-24 09:16:05
概述
public class FileService : WebService { [WebMethod] public int UploadFile(byte[] FileByte, String FileExtention) { FileStream stream = new FileStream(String.Format(@"D:\example.{0}", FileExtention),FileMode.CreateNew); stream.Write(FileByte, 0, FileByte.Length); stream.Close(); return FileByte.Length; } }
<Canvas Background="#FF333333"> <TextBox x:Name="txtFile" Height="30" Width="300" Canvas.Top="120" Canvas.Left="30" Style="{StaticResource textBoxStyle}"></TextBox> <Button x:Name="btnUpload" Width="60" Content="上 传" Height="30" Canvas.Left="340" Canvas.Top="120" Style="{StaticResource buttonStyle}" Click="OnUploadClick"></Button> <TextBlock x:Name="tblStatus" Canvas.Left="30" Canvas.Top="160" FontSize="14" Foreground="White" Text=""></TextBlock> </Canvas>
void OnUploadClick(object sender, RoutedEventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); if (openFile.ShowDialog() == DialogResult.OK) { String fileName = openFile.SelectedFile.Name; FileServiceSoapClient client = new FileServiceSoapClient(); client.UploadFileCompleted += new EventHandler<UploadFileCompletedEventArgs>(OnUploadFileCompleted); Stream stream = (Stream)openFile.SelectedFile.OpenRead(); stream.Position = 0; byte[] buffer = new byte[stream.Length + 1]; stream.Read(buffer, 0, buffer.Length); String fileExtention = fileName.Substring(fileName.IndexOf('.') + 1); client.UploadFileAsync(buffer, fileExtention); } } void OnUploadFileCompleted(object sender, UploadFileCompletedEventArgs e) { if (e.Error == null) { tblStatus.Text = "上传文件成功!"; } }运行程序后,选择一个文件并上传,如下图所示:
public class EmailService : WebService { [WebMethod] public bool Send(String fromAddress,String toAddress,String subject,String body) { try { MailMessage msg = new MailMessage(); msg.From = new MailAddress(fromAddress); msg.To.Add(new MailAddress(toAddress)); msg.Subject = subject; msg.Body = body; msg.IsBodyHtml = false; SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; smtp.Send(msg); return true; } catch { return false; } } }使用SmtpClient需要在Web.config文件中配置一下邮件服务器,这里使用Google的服务器,大家可以使用自己的Gmail帐号,如下代码所示:
<system.net> <mailSettings> <smtp> <network host="smtp.gmail.com" port="587" userName="[email]terrylee1218@gmail.com[/email]" password="19811218"/> </smtp> </mailSettings> </system.net>
<Grid x:Name="LayoutRoot" Background="#333333"> <Grid.RowDefinitions> <RowDefinition Height="70"></RowDefinition> <RowDefinition Height="50"></RowDefinition> <RowDefinition Height="50"></RowDefinition> <RowDefinition Height="200"></RowDefinition> <RowDefinition Height="50"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <local:TitleControl Grid.Row="0" Margin="8,8,8,8" Grid.ColumnSpan="2"></local:TitleControl> <TextBlock Text="收件人" Grid.Row="1" Style="{StaticResource textBlockStyle}"></TextBlock> <TextBlock Text="主 题" Grid.Row="2" Style="{StaticResource textBlockStyle}"></TextBlock> <TextBox x:Name="txtToEmailAddress" Grid.Row="1" Grid.Column="1" Width="440" Height="30" HorizontalAlignment="Left"></TextBox> <TextBox x:Name="txtSubject" Grid.Row="2" Grid.Column="1" Width="440" Height="30" HorizontalAlignment="Left"></TextBox> <TextBox x:Name="txtBody" Grid.Row="3" Grid.ColumnSpan="2" Width="500" HorizontalAlignment="Left" Height="200" Margin="100 0 0 0"></TextBox> <Button x:Name="btnSend" Grid.Row="4" Grid.Column="1" HorizontalAlignment="Left" Content="发 送" Style="{StaticResource buttonStyle}" Width="120" Height="30" Click="OnSendClick"></Button> </Grid>
void OnSendClick(object sender, RoutedEventArgs e) { // 发送邮件地址 String fromAddress = "[email]terrylee1218@gmail.com[/email]"; EmailServiceSoapClient client = new EmailServiceSoapClient(); client.SendCompleted += new EventHandler<SendCompletedEventArgs>(OnSendCompleted); client.SendAsync(fromAddress, this.txtToEmailAddress.Text, this.txtSubject.Text, this.txtBody.Text); } void OnSendCompleted(object sender, SendCompletedEventArgs e) { if (e.Result) { HtmlPage.Window.Alert("发送邮件成功!"); } else { HtmlPage.Window.Alert("发送邮件成功!"); } }运行后输入相关信息,并发送邮件,如下图所示: