Wednesday, May 25, 2011

Send email with attachment in ASP.NET?



First, you will need to import the System.Net.Mail namespace.


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

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailAddress SendFrom = new MailAddress(txtFrom.Text);
MailAddress SendTo = new MailAddress(txtTo.Text);

MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

MyMessage.Subject = txtSubject.Text;
MyMessage.Body = txtBody.Text;

Attachment attachFile = new Attachment(txtAttachmentPath.Text);
MyMessage.Attachments.Add(attachFile);

SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(MyMessage);

litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text=ex.ToString();
}
}

Create, Delete & Move Directory Using C#.Net?

Hi friend here I have written code for Creating, Delete and Move dirrctory.

In My createDirectory.aspx page I have use one text box and three button.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class CreateDirectory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        string FolderName = txtFolder.Text;
        string Path = Server.MapPath(FolderName);

        try
        {
            // Check if directory exists
            if (!Directory.Exists(Path))
            {
                // Create the directory.
                Directory.CreateDirectory(Path);
            }

        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string FolderName = txtFolder.Text;
        string Path = Server.MapPath(FolderName);

        try
        {
            // Check if directory exists
            if (Directory.Exists(Path))
            {
                // Delete the directory.
                Directory.Delete(Path);
            }

        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }
    protected void btnMove_Click(object sender, EventArgs e)
    {
        string FolderName = txtFolder.Text;
        string sPath = Server.MapPath(FolderName);
        string dPath = Server.MapPath("MoveItem\\" + FolderName);

        try
        {
            // Check if directory exists
            if (Directory.Exists(sPath))
            {
                // Move the directory.
                Directory.Move(sPath, dPath);
               
            }

        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }
}

Before deleting directory make sure directory is empty.

Monday, May 9, 2011

Script For Creating Directory And Folder Using C#.Net??


Here I just write code for create directory. Go through it it's a very simple example:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class CreateDirectory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        // Call function for creating a directory
        CreateDirectoryFunction(Server.MapPath("NewFolder"));
    }

    private void CreateDirectoryFunction(string NewDirectory)
    {
        try
        {
            // Check if directory exists
            if (!Directory.Exists(NewDirectory))
            {
                // Create the directory.
                Directory.CreateDirectory(NewDirectory);
            }
        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }
}