Hi
I want to create upload file and i want to read the same file which is uploaded in c#.net

I wrote a code like this can u please check it.

private void btnUpload_Click(object sender, EventArgs e)
        
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                filename = openFileDialog2.FileName;
                txtUpload.Text = filename;
            }
            else
            {
                MessageBox.Show("Please select a file");
            }
          //  openFileDialog2.ShowDialog();
           // txtUpload.Text = openFileDialog2.FileName;
            //openFileDialog1.Filter = "Microsoft Excel Workbooks (*.xls)*.xls";
            //if (openFileDialog1.ShowDialog() == DialogResult.OK)
            //{
              //    sFileName = openFileDialog1.FileName;
                //textBox1.Text = sFileName;
            //}
            //else
            //{
              //  MessageBox.Show("Please select a file");
            //}
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Book111.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";
            //string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\book1.xlsx;Extended Properties=""Excel 8.0;HDR=YES;""";
            //string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "+ filename + ";Extended Properties=Excel 8.0;HDR=YES;";
            OleDbConnection conn = new OleDbConnection(connectionString);
            string strSQL = "SELECT * FROM [Sheet1$]";

            OleDbCommand cmd = new OleDbCommand(strSQL, conn);
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            for (int ii = 0; ii < dt.Rows.Count; ii++)
            {
                DataRow dr = dt.Rows[ii];
                string emailIdds1 = dr[0].ToString();
                string[] emailIdfname = new string[10];
                emailIdfname = dr[0].ToString().Split('@');
                if (emailIdds1 != "")
                {
                    MailMessage mailMsg = new MailMessage();
                    Attachment attch = new Attachment(sFileName);
                    mailMsg.From = new MailAddress(txtFrom.Text);
                    mailMsg.To.Add(emailIdds1);
                    mailMsg.Subject = txtSubject.Text;
                    mailMsg.IsBodyHtml = true;
                    //mailMsg.BodyEncoding = Encoding.UTF8;
                    string fname = "Dear " + emailIdfname[0].ToString() + "&Family," + "<html><body></br></body></html>";
                    mailMsg.Body = fname + txtMessage.Text;
                    mailMsg.Attachments.Add(attch);
                    mailMsg.Priority = MailPriority.Normal;
                    // Smtp configuration
                    SmtpClient client = new SmtpClient();
                    client.Credentials = new NetworkCredential("srilekha.munigela@gmail.com", "suryam");
                    client.Port = 587; //or use 465            
                    client.Host = "smtp.gmail.com";
                    client.EnableSsl = true;
                    object userState = mailMsg;
                    try
                    {
                        //you can also call client.Send(msg)
                        //client.SendAsync(mailMsg, userState);
                        client.Send(mailMsg);

                    }
                    catch (SmtpException)
                    {
                        //Catch errors...
                    }

                }
            }

            mailSucc.Visible = true;


        }

am able to see the file and upload it but am unable to read the file can u please help me
as i wrote a code to split the name from "@"if numeric was present after the name it should not take it should read only the names not the numeric values can any one please help me by editing the code

When you say

but am unable to read the file

do you mean that the sent mail message has a reference to an attachement that can't be opened?

If so, be sure that in

Attachment attch = new Attachment(sFileName);

sFileName contains a valid file full path (Ie: "C:\some_folder\some_File.txt") and you have almost read permissions to the file, and the content is not blocked by the mail server.

About the @ split, if you need to 'remove' the numeric values from the splitted ones, you can write some thing like

private string RemoveNumbers(string inputString)
{
	// This is untested code
	var returnValue = new StringBuilder();
	const string numbers = "0123456789";
	for (int i = 0; i < inputString.Length; i++)
	{
		if (numbers.IndexOf(inputString.Substring(i, 1)) == -1)
			returnValue.Append(inputString.Substring(i, 1));
	}
	return returnValue.ToString();
}

then change the sentence

string fname = "Dear " + emailIdfname[0].ToString() + "&Family,"

to

string fname = "Dear " + [B]RemoveNumbers([/B]emailIdfname[0][B])[/B] + "&Family,"

Hope this helps

Hi
I want to read the excel file which is uploaded but am unable to read that can any one help me.

1.hi,
I have created like this to read but it is not reading the emails of the file.If i give the path directly it is taking can any one help me please.

private void btnUpload_Click(object sender, EventArgs e)
2.

3.
{
4.
if (openFileDialog2.ShowDialog() == DialogResult.OK)
5.
{
6.
filename = openFileDialog2.FileName;
7.
txtUpload.Text = filename;
8.
}
9.
else
10.
{
11.
MessageBox.Show("Please select a file");
12.
}
13.
// openFileDialog2.ShowDialog();
14.
// txtUpload.Text = openFileDialog2.FileName;
15.
//openFileDialog1.Filter = "Microsoft Excel Workbooks (*.xls)*.xls";
16.
//if (openFileDialog1.ShowDialog() == DialogResult.OK)
17.
//{
18.
// sFileName = openFileDialog1.FileName;
19.
//textBox1.Text = sFileName;
20.
//}
21.
//else
22.
//{
23.
// MessageBox.Show("Please select a file");
24.
//}
25.
}
26.

27.
private void btnSend_Click(object sender, EventArgs e)
28.
{
29.
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Book111.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";
30.
//string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\book1.xlsx;Extended Properties=""Excel 8.0;HDR=YES;""";
31.
//string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "+ filename + ";Extended Properties=Excel 8.0;HDR=YES;";
32.
OleDbConnection conn = new OleDbConnection(connectionString);
33.
string strSQL = "SELECT * FROM [Sheet1$]";
34.

35.
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
36.
DataSet ds = new DataSet();
37.
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
38.
da.Fill(ds);
39.
DataTable dt = ds.Tables[0];
40.
for (int ii = 0; ii < dt.Rows.Count; ii++)
41.
{
42.
DataRow dr = dt.Rows[ii];
43.
string emailIdds1 = dr[0].ToString();
44.
string[] emailIdfname = new string[10];
45.
emailIdfname = dr[0].ToString().Split('@');
46.
if (emailIdds1 != "")
47.
{
48.
MailMessage mailMsg = new MailMessage();
49.
Attachment attch = new Attachment(sFileName);
50.
mailMsg.From = new MailAddress(txtFrom.Text);
51.
mailMsg.To.Add(emailIdds1);
52.
mailMsg.Subject = txtSubject.Text;
53.
mailMsg.IsBodyHtml = true;
54.
//mailMsg.BodyEncoding = Encoding.UTF8;
55.
string fname = "Dear " + emailIdfname[0].ToString() + "&Family," + "<html><body></br></body></html>";
56.
mailMsg.Body = fname + txtMessage.Text;
57.
mailMsg.Attachments.Add(attch);
58.
mailMsg.Priority = MailPriority.Normal;
59.
// Smtp configuration
60.
SmtpClient client = new SmtpClient();
61.
client.Credentials = new NetworkCredential("srilekha.munigela@gmail.com", "suryam");
62.
client.Port = 587; //or use 465
63.
client.Host = "smtp.gmail.com";
64.
client.EnableSsl = true;
65.
object userState = mailMsg;
66.
try
67.
{
68.
//you can also call client.Send(msg)
69.
//client.SendAsync(mailMsg, userState);
70.
client.Send(mailMsg);
71.

72.
}
73.
catch (SmtpException)
74.
{
75.
//Catch errors...
76.
}
77.

78.
}
79.
}
80.

81.
mailSucc.Visible = true;
82.

83.

84.
}

1.hi,
I have created like this to read but it is not reading the emails of the file.If i give the path directly it is taking can any one help me please.

private void btnUpload_Click(object sender, EventArgs e)
2.

3.
{
4.
if (openFileDialog2.ShowDialog() == DialogResult.OK)
5.
{
6.
filename = openFileDialog2.FileName;
7.
txtUpload.Text = filename;
8.
}
9.
else
10.
{
11.
MessageBox.Show("Please select a file");
12.
}
13.
// openFileDialog2.ShowDialog();
14.
// txtUpload.Text = openFileDialog2.FileName;
15.
//openFileDialog1.Filter = "Microsoft Excel Workbooks (*.xls)*.xls";
16.
//if (openFileDialog1.ShowDialog() == DialogResult.OK)
17.
//{
18.
// sFileName = openFileDialog1.FileName;
19.
//textBox1.Text = sFileName;
20.
//}
21.
//else
22.
//{
23.
// MessageBox.Show("Please select a file");
24.
//}
25.
}
26.

27.
private void btnSend_Click(object sender, EventArgs e)
28.
{
29.
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Book111.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";
30.
//string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\book1.xlsx;Extended Properties=""Excel 8.0;HDR=YES;""";
31.
//string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "+ filename + ";Extended Properties=Excel 8.0;HDR=YES;";
32.
OleDbConnection conn = new OleDbConnection(connectionString);
33.
string strSQL = "SELECT * FROM [Sheet1$]";
34.

35.
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
36.
DataSet ds = new DataSet();
37.
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
38.
da.Fill(ds);
39.
DataTable dt = ds.Tables[0];
40.
for (int ii = 0; ii < dt.Rows.Count; ii++)
41.
{
42.
DataRow dr = dt.Rows[ii];
43.
string emailIdds1 = dr[0].ToString();
44.
string[] emailIdfname = new string[10];
45.
emailIdfname = dr[0].ToString().Split('@');
46.
if (emailIdds1 != "")
47.
{
48.
MailMessage mailMsg = new MailMessage();
49.
Attachment attch = new Attachment(sFileName);
50.
mailMsg.From = new MailAddress(txtFrom.Text);
51.
mailMsg.To.Add(emailIdds1);
52.
mailMsg.Subject = txtSubject.Text;
53.
mailMsg.IsBodyHtml = true;
54.
//mailMsg.BodyEncoding = Encoding.UTF8;
55.
string fname = "Dear " + emailIdfname[0].ToString() + "&Family," + "<html><body></br></body></html>";
56.
mailMsg.Body = fname + txtMessage.Text;
57.
mailMsg.Attachments.Add(attch);
58.
mailMsg.Priority = MailPriority.Normal;
59.
// Smtp configuration
60.
SmtpClient client = new SmtpClient();
61.
client.Credentials = new NetworkCredential("srilekha.munigela@gmail.com", "suryam");
62.
client.Port = 587; //or use 465
63.
client.Host = "smtp.gmail.com";
64.
client.EnableSsl = true;
65.
object userState = mailMsg;
66.
try
67.
{
68.
//you can also call client.Send(msg)
69.
//client.SendAsync(mailMsg, userState);
70.
client.Send(mailMsg);
71.

72.
}
73.
catch (SmtpException)
74.
{
75.
//Catch errors...
76.
}
77.

78.
}
79.
}
80.

81.
mailSucc.Visible = true;
82.

83.

84.
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.