Default.aspx.cs



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.Net;
using System.Net.Mail;

namespace WebPages
{
public partial class Default : System.Web.UI.Page
{
ArrayList lstEmails = new ArrayList();
ArrayList lstNames = new ArrayList();

protected void Page_Load(object sender, EventArgs e)
{
txtName.Focus();
lblPleaseEnter.Text = "Please enter at least " + lblMin.Text
+ " friend's Email addresses:";
lstEmails.Add(TextBox0);
lstEmails.Add(TextBox1);
lstEmails.Add(TextBox2);
lstEmails.Add(TextBox3);
lstEmails.Add(TextBox4);
lstEmails.Add(TextBox5);
lstEmails.Add(TextBox6);
lstEmails.Add(TextBox7);
lstEmails.Add(TextBox8);
lstEmails.Add(TextBox9);
lstNames.Add(TextBox10);
lstNames.Add(TextBox11);
lstNames.Add(TextBox12);
lstNames.Add(TextBox13);
lstNames.Add(TextBox14);
lstNames.Add(TextBox15);
lstNames.Add(TextBox16);
lstNames.Add(TextBox17);
lstNames.Add(TextBox18);
lstNames.Add(TextBox19);
for (int i = 0; i < int.Parse(lblMax.Text); i++)
{
((TextBox)lstEmails[i]).Visible = true;
((TextBox)lstNames[i]).Visible = true;
}

lblPleaseEnter.Text = "Please invite at least " + lblMin.Text
+ " of your friends";
}

protected void Button1_Click(object sender, EventArgs e)
{
if ((txtName.Text.Trim() == "") || (TextBox20.Text.Trim() ==
""))
{
Response.Write("<script>alert('Please enter your name and
email address!');</script>");
txtName.Focus();
}
else
{
ArrayList enteredEmails = new ArrayList();
for (int i = 0; i < int.Parse(lblMax.Text); i++)
{
if (((TextBox)lstEmails[i]).Text.Trim() != "")
{
if (!enteredEmails.Contains(((TextBox)lstEmails
[i]).Text.Trim()))
{
if (((TextBox)lstNames[i]).Text.Trim() == "")
{
Response.Write("<script>alert('Please add
names with email addresses!');</script>");
return;
}
else
enteredEmails.Add(((TextBox)lstEmails
[i]).Text.Trim());
}
else
{
if (lblMin.Text == "1")
{
Response.Write("<script>alert('Please add
at least 1 email address without repeating!');</script>");
return;
}
else
{
Response.Write("<script>alert('Please add
at least " + lblMin.Text + " different email addresses!');</script>");
return;
}
}
}
}
if (enteredEmails.Count < int.Parse(lblMin.Text))
{
if (lblMin.Text == "1")
{
Response.Write("<script>alert('Please add at
least one name and email address without repeating!');</script>");
return;
}
else
{
Response.Write("<script>alert('Please add at
least " + lblMin.Text + " different names and email addresses!');
</script>");
return;
}
}
else
{
string[] txtEmails = new string[enteredEmails.Count];
for (int i = 0; i < enteredEmails.Count; i++)
txtEmails[i] = enteredEmails[i].ToString();
if (chkSpam.Visible)
{
if (chkSpam.Checked)
{
string strReturn = SendInvitations
(txtName.Text, txtBody.Text, txtEmails, txtCodedString.Value, lstNames);
if (strReturn == "")
if (decodeThankYou(txtCodedString2.Value)
[enteredEmails.Count - int.Parse(lblMin.Text)].ToString().Substring(0,4)
== "http")
Response.Redirect(decodeThankYou
(txtCodedString2.Value)[enteredEmails.Count - int.Parse
(lblMin.Text)].ToString());
else
try
{
Server.Transfer(decodeThankYou
(txtCodedString2.Value)[enteredEmails.Count - int.Parse
(lblMin.Text)].ToString());
}
catch
{ }
else
Response.Write("<script>alert
('"+strReturn+"');</script>");
}
else
{
Response.Write("<script>alert('Please confirm
that you are not using this for spam!');</script>");
}
}
else
{
string strReturn = SendInvitations(txtName.Text,
txtBody.Text, txtEmails, txtCodedString.Value, lstNames);
if (strReturn == "")
if (decodeThankYou(txtCodedString2.Value)
[enteredEmails.Count - int.Parse(lblMin.Text)].ToString().Substring(0, 4)
== "http")
Response.Redirect(decodeThankYou
(txtCodedString2.Value)[enteredEmails.Count - int.Parse
(lblMin.Text)].ToString());
else
Server.Transfer(decodeThankYou
(txtCodedString2.Value)[enteredEmails.Count - int.Parse
(lblMin.Text)].ToString());
else
Response.Write("<script>alert('" + strReturn
+ "');</script>");
}
}
}
}

protected void btnClear_Click(object sender, EventArgs e)
{
for (int i = 0; i < int.Parse(lblMax.Text); i++)
((TextBox)lstEmails[i]).Text = "";
}

private string SendInvitations(string name, string body, string[]
emails, string codedString, ArrayList friend)
{
try
{
DecodeData dd = decode(codedString);
SmtpClient sc;
if (dd.smtp == "default")
sc = new SmtpClient();
else
sc = new SmtpClient(dd.smtp, dd.port);

if (dd.authUser != "")
{
sc.UseDefaultCredentials = false;
sc.Credentials = new NetworkCredential(dd.authUser,
dd.authPass);
}
try
{
for (int i = 0; i < emails.Length; i++)
{
if (dd.sender == "Visitor")
dd.sender = TextBox20.Text;
sc.Send(dd.sender, emails[i], dd.subject.Replace
("#VISITORNAME", name).Replace("#FRIENDNAME", ((TextBox)friend[i]).Text),
body);
if (dd.referral)
sc.Send(dd.sender, dd.email, "Tell A Friend
referral report", "Email sent form " + name +" ("+ TextBox20.Text+") to
" + ((TextBox)friend[i]).Text +" (" + emails[i] + ")\n\nSubject:\r\n" +
dd.subject.Replace("#VISITORNAME", name).Replace("#FRIENDNAME",
((TextBox)friend[i]).Text) + "\r\n\r\n" + "Body:\r\n" + body);
}
return "";
}
catch
{
return "Server error. Please check if all the
addresses really exist.";
}
}
catch
{
return "Server error. Please try again later.";
}
}

private DecodeData decode(string codedString)
{
DecodeData dd = new DecodeData();
dd.sender = codedString.Substring(0, codedString.IndexOf
("###"));
codedString = codedString.Substring(codedString.IndexOf
("###") + 3);
dd.subject = codedString.Substring(0, codedString.IndexOf
("###"));
codedString = codedString.Substring(codedString.IndexOf
("###") + 3);
if (codedString.Substring(0, codedString.IndexOf("###")) ==
"true")
dd.referral = true;
else
dd.referral = false;
codedString = codedString.Substring(codedString.IndexOf
("###") + 3);
dd.smtp = codedString.Substring(0, codedString.IndexOf
("###"));
codedString = codedString.Substring(codedString.IndexOf
("###") + 3);
dd.port = int.Parse(codedString.Substring(0,
codedString.IndexOf("###")));
codedString = codedString.Substring(codedString.IndexOf
("###") + 3);
if (codedString.IndexOf("###") == -1)
{
dd.email = codedString.Substring(0);
dd.authUser = "";
dd.authPass = "";
}
else
{
dd.email = codedString.Substring(0, codedString.IndexOf
("###"));
codedString = codedString.Substring(codedString.IndexOf
("###") + 3);
dd.authUser = codedString.Substring(0,
codedString.IndexOf("###"));
codedString = codedString.Substring(codedString.IndexOf
("###") + 3);
dd.authPass = codedString.Substring(0);
}
return dd;
}

private ArrayList decodeThankYou(string codedString)
{
ArrayList al = new ArrayList(int.Parse(lblMax.Text) -
int.Parse(lblMin.Text) + 1);
for (int i = 0; i < int.Parse(lblMax.Text) - int.Parse
(lblMin.Text) + 1; i++)
{
al.Add(codedString.Substring(0, codedString.IndexOf
("###")));
codedString = codedString.Substring(codedString.IndexOf
("###") + 3);
}
return al;
}

protected void txtName_TextChanged(object sender, EventArgs e)
{

}

}

class DecodeData
{
private string _sender;
private string _subject;
private bool _referral;
private string _smtp;
private int _port;
private string _email;
private string _authUser;
private string _authPass;

public string sender
{
get { return _sender; }
set { _sender = value; }
}

public string subject
{
get { return _subject; }
set { _subject = value; }
}

public bool referral
{
get { return _referral; }
set { _referral = value; }
}

public string smtp
{
get { return _smtp; }
set { _smtp = value; }
}

public int port
{
get { return _port; }
set { _port = value; }
}

public string email
{
get { return _email; }
set { _email = value; }
}

public string authUser
{
get { return _authUser; }
set { _authUser = value; }
}

public string authPass
{
get { return _authPass; }
set { _authPass = value; }
}
}
}
.