Showing posts with label WebBuild. Show all posts
Showing posts with label WebBuild. Show all posts

Saturday, March 22, 2008

Sending Email In ASP.NET 2.0

Step 1: Declare the System.Net.Mail namespace

using System.Net.Mail;

Step 2: Create a MailMessage object. This class contains the actual message you want to send. There are four overloaded constructors provided in this class. We will be using

public MailMessage ( string from, string to, string subject, string body );

The constructor of this MailMessage class is used to specify the sender email, receiver email, subject, body.

MailMessage message = new MailMessage            ("abc@somedomain.com","administrator@anotherdomain.com","Testing","This is a test mail");

Step 3: To add an attachment to the message, use the Attachment class.

string fileAttach = Server.MapPath("myEmails") + "\\Mypic.jpg";
Attachment attach = new Attachment(fileAttach);
message.Attachments.Add(attach);

Step 4:After creating a message, use the SmtpClient to send the email to the specified SMTP server. I will be using ‘localhost’ as the SMTP server.

SmtpClient client = new SmtpClient("localhost");
client.Send(message);
 
Additionally, if required, you
 
client.Timeout = 500;
// Pass the credentials if the server requires the client to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;

 

That’s it. It’s that simple.

To configure SMTP configuration data for ASP.NET, you would add the following tags to your web.config file.

<system.net>

    <mailSettings>

      <smtpfrom="abc@somedomain.com">

        <networkhost="somesmtpserver"port="25"userName="name"password="pass"defaultCredentials="true" />

      </smtp>

    </mailSettings>

</system.net>

References :

http://msdn2.microsoft.com/En-US/library/system.net.mail.mailmessage.aspx

Paging and Sorting in GridView without using Datasource control

 

<asp:GridView ID="gridView" OnPageIndexChanging="gridView_PageIndexChanging" 
OnSorting="gridView_Sorting" runat="server" />
private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
   string newSortDirection = String.Empty;
   switch (sortDirection)
   {
      case SortDirection.Ascending:
         newSortDirection = "ASC";
         break;
      case SortDirection.Descending:
         newSortDirection = "DESC";
         break;
   }
   return newSortDirection
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   gridView.PageIndex = e.NewPageIndex;
   gridView.DataBind();
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dataTable = gridView.DataSource as DataTable;
   if (dataTable != null)
   {
      DataView dataView = new DataView(dataTable);
      dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
      gridView.DataSource = dataView;
      gridView.DataBind();
   }
}

Source

Save and Retrieve Images from the Database using ASP.NET

Source

Highlight a Row in GridView without a postback using ASP.NET and JavaScript

Add "OnRowCreated" event to the GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" OnRowCreated="GridView1_RowCreated">
 
...
 
</asp:GridView>

The event code:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Attributes.Add("onMouseOver", "this.style.background='#eeff00'");
    e.Row.Attributes.Add("onMouseOut", "this.style.background='#ffffff'");       
}
Source