瑞星卡卡安全论坛

首页 » 技术交流区 » 反病毒/反流氓软件论坛 » 菜鸟学堂 » c#!每天进步一点点(原创)
风云一号 - 2005-1-15 15:20:00
此系列文章谨献给c#初学者:
此系列安装VS 2003(简体中文或E文)+SQL Server2000(加Sp3)

第一课:利用c#来发送邮件(B/S为例)
    第一步:新建一c#工程first_cs01,删除默认WebForm1.aspx(养成好习惯啊)新建 一页面SendMail.aspx,依次创建六个控件:
    两个label,两个TextBox,两个Button,如图


附件: 1271682005115152008.jpg
风云一号 - 2005-1-15 15:29:00
第二步:修改两个TetxBox的属性(也就是ID啦)为txtTo,txtFrom,然后,双击“发送”按钮,进入后台,在private void Button1_Click(object sender, System.EventArgs e)按钮事件中添加:
下列代码:
MailMessage mailObj = new MailMessage();
if (this.txtFrom.Text.Length>0 && this.txtTo.Text.Length>0)
{
mailObj.To=this.txtTo.Text;
mailObj.From=this.txtFrom.Text;
}
mailObj.Subject = "精采笑话"; //邮件主题
mailObj.Body = "猪!你已中毒! 哈哈 "; //邮件内容
mailObj.BodyFormat = MailFormat.Html; //邮件格式
mailObj.Priority = MailPriority.High; //邮件优先度
mailObj.Attachments.Add(new MailAttachment("c:\\swf\\000.bmp")); //附件名称,可有N多。
SmtpMail.Send(mailObj); //调用系统组件发送Mail
Response.Write("发送邮件成功!");
水瀑 - 2005-1-15 15:35:00
谢谢版主呀,这样的最好天天有,我支持你呀。
风云一号 - 2005-1-15 15:37:00
别急!很快就好了! 现在编译,生成解决方案!
哈哈~! 编译错误, !

咦,忘了声明引用dll了!
在 SendMail.cs文件顶部添加
using System.Web.Mail;
using System.Text;
两行即可!编译成功了吧!
在地址栏内敲入http://localhost/first_cs01/SendMail.aspx就可以浏览我们的成果了!
水瀑 - 2005-1-15 15:44:00
版主,我有一个问题,怎么样才能保存我想看的帖子呢?这样就不用以后找的时候麻烦了
风云一号 - 2005-1-15 15:44:00
怎么没有看到"发送成功"的提示?
咦! 又忘了一件大事:
IIS的设置应该启用Smtp(如果没安装,请用win2000安装盘进行安装)应该如下图(没有加入域的机器不会显示域)

附件: 1271682005115154458.jpg
水瀑 - 2005-1-15 15:54:00
你发一个我发一个,哈哈,快回答我的问题,别告诉我没有那个工能就行了
风云一号 - 2005-1-15 15:57:00
如法泡制,在第二个按钮事件中添加下列代码:
MailMessage mail = new MailMessage();
if (this.txtFrom.Text.Length>0 && this.txtTo.Text.Length>0)
{
mail.To=this.txtTo.Text;
mail.From=this.txtFrom.Text;
}

try
{
mail.Subject = "SMTP邮件!";
mail.Body = "测试文本正文";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret"); //set your password here
//SmtpMail.SmtpServer = "192.168.72.138"; //your real server goes here
SmtpMail.Send( mail );
//SmtpMail.Close();
string strInfo="<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
Response.Write(strInfo);
Response.Write("发送SMTP邮件成功!");

}
catch(Exception ex)
{
Response.write(ex);
Response.End();
}
水瀑 - 2005-1-15 16:03:00
大部分看明白了,不过,还是有一些不明白,看来没学到就是不行呀。
风云一号 - 2005-1-15 16:06:00
有些人可能很奇怪,怎么多了个try ………catch
问得好! 这是.net自带的错误捕获模块,它负责把所有可能出现的错误Catch到,然后暴露给用户(也就是你啦,怎么样很爽吧!)当然如果程序写的再灵活一点:
把邮件的标题和内容全部在aspx文件中用TextyBox控件来呈现给用户,由用户来自由输入,岂不更爽!?
风云一号 - 2005-1-15 16:16:00
今天主要简介了两种了送mail的方式,都是调用的.net自带的System.Web.Mail;这个类,用起来还不错吧!当做如果你的机器是企业内部的邮件服务器,设置就稍微复杂一些!不在本文研究之列,在举的朋友请加
msn:
zdownmoon@hotmail.com
安德倚天剑 - 2005-1-16 13:43:00
soko1 - 2005-1-16 14:46:00
佩服啊
endurer - 2005-1-16 20:54:00
学习。。。
小程 - 2005-1-18 16:32:00
继续呀
无奈的平静 - 2005-1-21 7:37:00
顶一下呀!
gjinrong - 2005-1-21 22:38:00
收藏
易碎物品的LG - 2005-1-24 21:03:00
我说大家,都别发什么顶啊,什么的,让楼主每次发几个好不好?光看你们这些无聊的回话了!哎!
liyoubo - 2005-1-27 11:49:00
我也来给大家一点点东西看看,这个是我在网上看到的,不是我自己的原创,或许你看过,就不要说三到四的,偶是菜鸟来的哈!!

用Visual C#实现文件下载功能:
实现原理:
  程序实现的原理比较简单,主要用到了WebClient类和FileStream类。其中WebClient类处于System.Net名字空间中,该类的主要功能是提供向URI标识的资源发送数据和从URI标识的资源接收数据的公共方法。我们利用其中的DownloadFile()方法将网络文件下载到本地。然后用FileStream类的实例对象以数据流的方式将文件数据写入本地文件。这样就完成了网络文件的下载。
实现步骤:
  首先,打开Visual Studio.Net,新建一个Visual C# Windows应用程序的工程,不妨命名为"MyGetCar"。
接着,布置主界面。我们先往主窗体上添加如下控件:两个标签控件、两个文本框控件、一个按钮控件以及一个状态栏控件。最终的主窗体如下图所示:




  完成主窗体的设计,我们接着完成代码的编写。

  在理解了基本原理的基础上去完成代码的编写是相当容易。程序中我们主要用到的是WebClient类,不过在我们调用WebClient类的实例对象前,我们需要用WebRequest类的对象发出对统一资源标识符(URI)的请求。

try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
}

  这是一个try-catch语句,try块完成向URI的请求,catch块则捕捉可能的异常并显示异常信息。其中的URLAddress为被请求的网络主机名。

  在请求成功后,我们就可以运用WebClient类的实例对象中的DownloadFile()方法实现文件的下载了。其函数原型如下:

public void DownloadFile( string address, string fileName);

  其中,参数address为从中下载数据的 URI,fileName为要接收数据的本地文件的名称。
之后我们用OpenRead()方法来打开一个可读的流,该流完成从具有指定URI的资源下载数据的功能。其函数原型如下:

 ublic Stream OpenRead(string address);
 
  其中,参数address同上。

  最后就是新建一个StreamReader对象从中读取文件的数据,并运用一个while循环体不断读取数据,只到读完所有的数据。

  还有在使用以上方法时,你将可能需要处理以下几种异常:

   WebException:下载数据时发生错误。

   UriFormatException:通过组合 BaseAddress、address 和 QueryString 所构成的 URI 无效。

  这部分的代码如下:(client为WebClient对象,在本类的开头处声明即可)

statusBar.Text = "开始下载文件...";
client.DownloadFile(URLAddress,fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
statusBar.Text = "正在接收数据...";
while(allmybyte>0)
{
int m = str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;

startmbyte+=m;
allmybyte-=m;
}

  完成了文件数据的读取工作后,我们运用FileStream类的实例对象将这些数据写入本地文件中:

FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);

  这样,程序主体部分的代码已经完成了,不过要完成全部程序还需要一些工作。由于在程序接收网络文件数据的时候运用到了while循环体,这样会很占程序资源,表现的形式就是主窗体不能自由移动。为了解决这个问题,我们在程序中用到了多线程机制。我们在响应按钮的事件中新建一个线程,该线程就是用来实现网络文件下载功能的。如此,文件下载的线程和程序主线程并存,共享进程资源,使得程序顺畅运行。这样,我们在按钮控件的消息响应函数里添加如下代码:

Thread th = new Thread(new ThreadStart(StartDownload));
th.Start();

  该线程的实现函数就是StartDownload(),而上面介绍的那些代码就是这个函数的主体部分。

  最后,因为程序中运用到了WebRequest、WebClient、FileStream、Thread等类,所以最重要的就是在程序的开始处添加如下名字空间:

using System.Net;
using System.IO;
using System.Threading;
liyoubo - 2005-1-27 11:54:00
下面就是程序的源代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Threading;

namespace MyGetCar
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox srcAddress;
private System.Windows.Forms.TextBox tarAddress;
private System.Windows.Forms.StatusBar statusBar;
private System.Windows.Forms.Button Start;

private WebClient client = new WebClient();

///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
liyoubo - 2005-1-27 11:55:00
#region Windows Form Designer generated code
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.srcAddress = new System.Windows.Forms.TextBox();
this.tarAddress = new System.Windows.Forms.TextBox();
this.statusBar = new System.Windows.Forms.StatusBar();
this.Start = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 0;
this.label1.Text = "文件地址:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 1;
this.label2.Text = "另存到:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// srcAddress
//
this.srcAddress.Location = new System.Drawing.Point(80, 32);
this.srcAddress.Name = "srcAddress";
this.srcAddress.Size = new System.Drawing.Size(216, 21);
this.srcAddress.TabIndex = 2;
this.srcAddress.Text = "";
//
// tarAddress
//
this.tarAddress.Location = new System.Drawing.Point(80, 72);
this.tarAddress.Name = "tarAddress";
this.tarAddress.Size = new System.Drawing.Size(216, 21);
this.tarAddress.TabIndex = 3;
this.tarAddress.Text = "";
//
// statusBar
//
this.statusBar.Location = new System.Drawing.Point(0, 151);
this.statusBar.Name = "statusBar";
this.statusBar.Size = new System.Drawing.Size(312, 22);
this.statusBar.TabIndex = 4;
//
// Start
//
this.Start.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Start.Location = new System.Drawing.Point(216, 112);
this.Start.Name = "Start";
this.Start.Size = new System.Drawing.Size(75, 24);
this.Start.TabIndex = 5;
this.Start.Text = "开始下载";
this.Start.Click += new System.EventHandler(this.Start_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(312, 173);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.Start,
this.statusBar,
this.tarAddress,
this.srcAddress,
this.label2,
this.label1});
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "文件下载器";
this.ResumeLayout(false);

}
#endregion
liyoubo - 2005-1-27 11:56:00
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void StartDownload()
{
Start.Enabled = false;
string URL = srcAddress.Text;
int n = URL.LastIndexOf('/');
string URLAddress = URL.Substring(0,n);
string fileName = URL.Substring(n+1,URL.Length-n-1);
string Dir = tarAddress.Text;
string Path = Dir+'\\'+fileName;

try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
}

try
{
statusBar.Text = "开始下载文件...";
client.DownloadFile(URLAddress,fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
statusBar.Text = "正在接收数据...";
while(allmybyte>0)
{
int m = str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;

startmbyte+=m;
allmybyte-=m;
}

FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
str.Close();
fstr.Close();

statusBar.Text = "下载完毕!";
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
statusBar.Text = "";
}

Start.Enabled = true;
}


private void Start_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(StartDownload));
th.Start();
}
}
}
youjay - 2005-1-27 12:18:00
斑竹啊,偶程序看懂啦,就是不了解c#,使用的语言很像java语言
youjay - 2005-1-27 12:19:00
还有,vs是什么?
liyoubo - 2005-1-27 12:29:00
visual studio
你指的是不是这个?
intel - 2005-2-12 8:49:00
厉害
Handsome_001 - 2005-2-17 13:14:00
VS 2003(简体中文或E文)
是什么?
我小交交我 - 2005-2-18 23:38:00
说了这么多!
好是不懂
怪才阿伟 - 2005-2-21 8:52:00
顶一下
comet - 2005-2-25 15:49:00
不错哇
123
查看完整版本: c#!每天进步一点点(原创)