asp.net 2.0 create and use Captcha image using webservice C# and send email, or contact form :-
1. Create a webservice:- Captcha.ashx
<%@ WebHandler Language="C#" Class="Captcha" %>
using System;
using System.Web;
using System.Drawing;
using System.IO;
using System.Web.SessionState;
public class Captcha : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest (HttpContext context) {
Bitmap bmpOut = new Bitmap(45, 15);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.Black, 0, 0, 45, 20);
g.DrawString(context.Session["Captcha"].ToString(), new Font("Verdana", 10), new SolidBrush(Color.White), 0, 0);
MemoryStream ms = new MemoryStream();
bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] bmpBytes = ms.GetBuffer();
bmpOut.Dispose();
ms.Close();
context.Response.BinaryWrite(bmpBytes);
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frndshr.aspx.cs" Inherits="frndshr" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="text-align: center">
<table style="width: 70%">
<tr>
<td align="right">
</td>
<td align="left">
</td>
<td align="left" style="width: 110px">
</td>
<td align="left" style="width: 373px">
</td>
</tr>
<tr>
<td align="right" style="height: 28px">
<asp:Label ID="Label3" runat="server" Text="Your Name:"></asp:Label></td>
<td align="left" style="height: 28px">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
<td align="right" style="height: 28px; width: 110px;">
<asp:Label ID="Label1" runat="server" Text="Your Email:"></asp:Label></td>
<td align="left" style="width: 373px; height: 28px">
<asp:TextBox ID="TextBox1" runat="server" Width="263px"></asp:TextBox></td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label4" runat="server" Text="Friend's Name:" Width="100px"></asp:Label></td>
<td align="left">
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td>
<td align="right" style="width: 110px">
<asp:Label ID="Label2" runat="server" Text="Friend's Email:" Width="94px"></asp:Label></td>
<td align="left" style="width: 373px">
<asp:TextBox ID="TextBox2" runat="server" Width="263px"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate="TextBox2"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="height: 26px">
</td>
<td align="right" style="height: 26px" valign="top">
<asp:Image ID="imCaptcha" ImageUrl="Captcha.ashx" runat="server" /></td>
<td align="left" style="height: 26px; width: 110px;" valign="top">
<asp:TextBox ID="txtVerify" runat="server" Width="80px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtVerify"></asp:RequiredFieldValidator>
</td>
<td style="height: 26px; width: 373px;" align="left">
<asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />
<asp:Label ID="labmsg" runat="server" Width="158px"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" CausesValidation="False">Go Back</asp:LinkButton>
<asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="txtVerify"
ErrorMessage="Wrong Verification Code.Please Re-enter." OnServerValidate="CAPTCHAValidate"></asp:CustomValidator></td>
</tr>
<tr>
<td style="height: 26px">
</td>
<td align="right" style="height: 26px" valign="top">
</td>
<td align="left" style="width: 110px; height: 26px" valign="top">
</td>
<td align="left" style="width: 373px; height: 26px">
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
code behind
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
public partial class frndshr : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
VerificationTxt();
}
}
public void VerificationTxt()
{
Random ran = new Random();
int no = ran.Next(1000, 2000);
Session["Captcha"] = no.ToString();
}
protected void CAPTCHAValidate(object source, ServerValidateEventArgs args)
{
if (Session["Captcha"] != null)
{
if (txtVerify.Text != Session["Captcha"].ToString())
{
VerificationTxt();
args.IsValid = false;
return;
}
}
else
{
VerificationTxt();
args.IsValid = false;
return;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}
sndmail();
VerificationTxt();
}
void sndmail()
{
MailMessage oMessage = new MailMessage();
oMessage.To = TextBox2.Text;
oMessage.From = TextBox1.Text ;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//sb.Append(yrnamtxt.Text);
//sb.Append(" Email ");
// sb.Append("I found this from abc site , great deals");
//sb.Append("Subject ");
//sb.Append("Deal Found");
oMessage.Subject = "Great Finds";
oMessage.Body = "I found this from abc site , great deals";
//oMessage.Attachments = txtBody.Text;
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "mail.Domain.com";
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "webmaster@Domain.com";
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "zzzzzzzz";
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
System.Web.Mail.SmtpMail.SmtpServer = "mail.visli.com";
try
{
oMessage = oMessage;
System.Web.Mail.SmtpMail.Send(oMessage);
labmsg.Text = "Message Send";
}
catch (Exception Ex)
{
labmsg.Text = ("Unable to send mail! " + Ex.Message);
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
}
}
No comments:
Post a Comment