Skip to main content

Posts

Showing posts from April, 2009

How to Send Emails through ASP.NET 2.0 and Above in Vista

Sending emails in a Web Application has been a crucial task. ASP.NET provides method to send regular and html emails with attachments. A SMTP relay server is required to send emails from web applications. We can use the smtp.gmail.com service to send emails by using an authenticated user account. The following code shows how to send normal/regular emails by making changes in web.config file: a. Web.config File changes Make the following changes in the web.config file b. OnButton_Click MailMessage mail=new MailMessage(); mail.To.Add( receiver@gmail.com ); mail.From=new MailAddress( xx@gmail.com ); mail.Subject="Hi this is a test message"; mail.Body="Welcome to ASP.NET email sending"; SmtpClient smtp=new SmtpClient(); smtp.EnableSsl=true; smtp.Send(mail); This is a simple method of sending emails through ASP.NET pages.

How to handle events on Buttons embedded in the User Controls and call them in ASP.NET

In order to capture events on buttons in ASP.NET web forms that are part of a WebUser Control, the following method without delegate can be used: a. Code behind of Web User Control //Create an event handler public event EventHandler OnButtonClick; //Create an new event on the click of button public void Button1_Click(object sender, EventArgs e) { this.OnButtonClick(this, new EventArgs()); } b. Code behind of Web Form of ASP.NET //On the page load event use the following code this.WebUserControl1.OnButtonClick+=new EventHandler(WebUserControl1.OnButtonClick); //Define the method that handles the event now void WebUserControl1.ButtonClick(object sender, EventArgs e) { ///custom code goes here. } This is the easiest method of generating an event and capturing the same in the main Web Form without Delegates. We can do the same with the help of delegates. I will cover the same in the coming post.