Powered By Blogger

Wednesday, December 30, 2009

send mail in asp.net c#

web.config










aspx.cs

string ToMail, FromMail, MailBody, host, portNo, password;

page load:

FromMail = ConfigurationManager.AppSettings["mailFrom"];
password = ConfigurationManager.AppSettings["password"];
host = ConfigurationManager.AppSettings["host"];
portNo = ConfigurationManager.AppSettings["port"];
ToMail = Request.QueryString[1].ToString();
MailBody="test mail";

button click


MailMessage mailMessage = new MailMessage(FromMail, ToMail, "Hello ! " + ToMail + ", Mr/Mrs" + ToMail + "Has Replied you", MailBody);
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
NetworkCredential networkCredential = new NetworkCredential(FromMail, password);
smtpClient.Host = host;
mailMessage.Priority = MailPriority.High;
if (!string.IsNullOrEmpty(portNo))
{
smtpClient.Port = Convert.ToInt32(portNo);
smtpClient.EnableSsl = true;
}
smtpClient.Credentials = networkCredential;
smtpClient.Send(mailMessage);


------------------------------------------------------------------------------------------



using System.Net;
using System.Net.Mail;
using System.Web.Configuration;

Method I
-----------

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
System.Net.NetworkCredential credential = new System.Net.NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);

int i = 0;
//Create the SMTP Client
SmtpClient client = new SmtpClient();
client.Host = settings.Smtp.Network.Host;
client.Credentials = credential;
HttpResponse response = HttpContext.Current.Response;
MailMessage email = new MailMessage();

email.IsBodyHtml = true;
email.From = new MailAddress(FromMail);
email.To.Add("ranjith@isolve.co.in");
// email.To.Add(ToMail);

//email.To.Add(strToEmail)
email.Subject = SubjecT;
email.IsBodyHtml = true;
email.Body = MailBody;
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

try
{
client.Send(email);
}
catch (Exception exc)
{
response.Write("Send failure: " + exc.ToString());
}


WEB.CONFIG
============


<appSettings>
<add key="SenderUsername" value="xx@gmail.com"/>
<add key="SenderPassword" value="xx"/>
<add key="HostIP" value="***.***.***.***"/>

</appSettings>





============================================================

Method II
-------------

public void SendMail(string FrmMail, string ToMail, string MailBody, string SubjecT)
{
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
System.Net.NetworkCredential credential = new System.Net.NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);

int i = 0;
//Create the SMTP Client
SmtpClient client = new SmtpClient();
client.Host = settings.Smtp.Network.Host;
client.Credentials = credential;
HttpResponse response = HttpContext.Current.Response;
MailMessage email = new MailMessage();



email.IsBodyHtml = true;
email.From = new MailAddress(FrmMail);
email.To.Add(ToMail);
//email.To.Add(ToMail);

//email.To.Add(strToEmail)
email.Subject = SubjecT;
email.IsBodyHtml = true;
email.Body = MailBody;
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

try
{
client.Send(email);

}
catch (Exception exc)
{
response.Write("Send failure: " + exc.ToString());

}
}


*FIREWALL SHOULD BE OFF

-------------------------------------------------------------------

getting values from popup window using javascript


Getting values from popup window using javascript

parent page:

popupform.htm:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Opener</title>
<script type='text/javascript'>
function valideopenerform(){
var popy= window.open('popup.htm','popup_form','menubar=no,status=no,top=25%,left=25;')
alert (popy.window.document.URL);
}
</script>
</head>

<body>
<form name='openerform' id='openerform' >
<input type='text' id='text1' name='text1' />
<input type='button' value='go' onclick='valideopenerform()' />
</form>

</body>

</html>

which page to be pop up:

popup.htm


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Opener</title>
<script type='text/javascript'>
function validepopupform(){
window.opener.document.openerform.text1.value=document.popupform.text2.value;
self.close();
}
</script>

</head>

<body>
<form id='popupform' name='popupform' >
<input type='text' id='text2' name='text2' />
<input type='button' value='go' onclick='validepopupform()' />
</form>

</body>

</html>


cookies in asp.net example c#


Cookies in asp.net example c#

cookie.aspx


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btn_cookie" runat="server" OnClick="btn_cookie_Click" Text="Create Cookie" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btn_viewcookie" runat="server" OnClick="btn_viewcookie_Click" Text="View Cookie" />

cookie.aspx.cs


HttpCookie cook;

protected void btn_cookie_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
cook = new HttpCookie("username");
cook.Value = TextBox1.Text.Trim();
cook.Expires = DateTime.Now.AddMinutes(1);
Response.Cookies.Add(cook);
}
else
Response.Write("Enter Value");
}
protected void btn_viewcookie_Click(object sender, EventArgs e)
{
cook = Request.Cookies["username"];

if (cook != null)
{
Label1.Text = cook.Value.ToString();
}
else
{
Label1.Text = "Cookie Not Available or Expired.";
}
}





Datalist asp.net c# example

In SQL-Server

create table emp(empid int,empname varchar(50),empdesc varchar(500))


datalist.aspx.cs

<asp:DataList Width="43%" ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<table width="50%">
<tr bgcolor="silver">
<td width="50%">
<asp:Label ID="lbl_1" runat="server" Text="first"></asp:Label>
</td>
<td width="50%">
<asp:Label ID="lbl_2" runat="server" Text="second"></asp:Label>
</td>

</tr>
<tr bgcolor="lime">
<td width="100%" colspan="2">
<asp:TextBox ID="txt_1" TextMode="multiLine" runat="server"></asp:TextBox>
</td>

</tr>

</table>

</ItemTemplate>
</asp:DataList>


datalist.cs

SqlDataAdapter da;
DataTable dt= new DataTable();
int counter = 0;

in pageload event call this bind() method:

bind()
{
SqlConnection con = new SqlConnection("server=.;database=demo;integrated security=true;");
con.Open();
da = new SqlDataAdapter("select * from emp", con);
da.Fill(dt);
con.Close();
DataList1.DataSource = dt;
DataList1.DataBind();
}

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
Label lbl1 = (Label)e.Item.FindControl("lbl_1");
lbl1.Text = dt.Rows[counter][0].ToString();
Label lbl2 = (Label)e.Item.FindControl("lbl_2");
lbl2.Text = dt.Rows[counter][1].ToString();
TextBox txt = (TextBox)e.Item.FindControl("txt_1");
txt.Text = dt.Rows[counter][2].ToString();
counter++;
}