学习篇:验证码和邮件发送

1.说到验证码,不得不说说验证码的作用。首先,验证码不是防人的,而是防止机器程序的。防止机器程序的暴力破解所用的一个方法。

下面就是一般处理程序 版的,生成验证码。

一般处理程序版
public class MyValidateCode : IHttpHandler,IRequiresSessionState
{

HttpContext context;

public void ProcessRequest(HttpContext context1)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
this.context = context1;
CreateCheckCodeImg(GenerateCheckCode());

}

private string GenerateCheckCode()
{
int number;
char code;
string checkCode = string.Empty;

Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number%2==0)
{
code = (char)('0' + (char)(number % 10));
}
else
{
code = (char)('0' + (char)(number % 26));
}
checkCode += code.ToString();
}
context.Session.Add("vCode", checkCode);
return checkCode;
}

private void CreateCheckCodeImg(string checkCode)
{
if (checkCode==null||checkCode.Trim()==string.Empty)
{
return;
}
Bitmap image = new Bitmap((int)Math.Ceiling(checkCode.Length * 12.5), 22);
Graphics g = Graphics.FromImage(image);

try
{
Random random = new Random();

g.Clear(Color.White);

for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);

g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}

Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);

for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);

image.SetPixel(x, y, Color.FromArgb(random.Next()));
}

g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());

}
finally
{
g.Dispose();
image.Dispose();
}
}

首先这个不是我写的,只是照着别人原创的敲上了一遍,因为一直只是会用,却很少看是咋个实现。

给验证码注册个单击事件,点击可改变验证码。以下就是单击事件中的代码。

function ChangeCode() {
            var img = document.getElementById("imgCode").src = "/ashx/MyValidateCode.ashx?date=" + new Date().getMilliseconds();
        }

  

2.邮件发送

  MailMessage mailMsg = new MailMessage();//两个类,别混了,要引入System.Net这个Assembly
            mailMsg.From = new MailAddress("136***404@qq.com", "admin");//源邮件地址(发件人) 
            mailMsg.To.Add(new MailAddress("244***0021@qq.com"));//目的邮件地址。可以有多个收件人
            mailMsg.Subject = "2月18号 给你发来邮件";//发送邮件的标题 
            mailMsg.Body = "欢迎访问 2月18号 博客~!";//发送邮件的内容 
            SmtpClient client = new SmtpClient("smtp.qq.com");//smtp.163.com,smtp.qq.com , smtp.126.com
            client.Credentials = new NetworkCredential("136***404", "he******he");
            client.Send(mailMsg);

  当然,发送邮件远不止这一点点的学问,还有好多的属性,比如 IsBodyHtml 设置为True 就可以发送HTML 形式的邮件,还有发送 图片 ,附件 等形式的。。。

我本菜鸟级别,请各位大师 莫来拍砖。点到为止,点到为止~~~

   

posted @ 2012-10-19 00:32  2月18号  阅读(1334)  评论(1编辑  收藏  举报