Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I Want to do? I want to send email to the person who's mail id enter in a excel sheet. when u send the email to all the mail id no body can check another person mail id. I just want to used the BCC Concept in my mail Demo with attachment of multiply file.another thing i want to do is this the name of the person who is mention in front of mail id in excel file that is also mention in the mail.........
Posted
Comments
ZurdoDev 11-Jan-13 7:59am    
Your description is not entirely clear; however, where are you stuck?

1 solution

I have not tested this snippet to make sure it works...mainly because i think working off of an excel worksheet as database is pointless.

But anyways according to your description (not entirely clear) i think this is what you were looking for, again not tested so may have issues. You will need to add the com reference for Excel.

C#
using Excel = Microsoft.Office.Interop.Excel;
using System.Net.Mail;

Excel.Application excel = null;
Excel.Workbook wkbk = null;

excel = new Excel.Application();

wkbk = excel.Workbooks.Open(@"c:\path\to\excel\file.xls",
							true,
							true,
							Type.Missing,
							Type.Missing,
							Type.Missing,
							Type.Missing,
							Type.Missing,
							Type.Missing,
							false,
							Type.Missing,
							Type.Missing,
							Type.Missing,
							Type.Missing,
							Type.Missing);



Excel.Worksheet sheet = wkbk.Sheets["Sheet1"] as Excel.Worksheet;

Excel.Range getRange = null;


if (sheet != null)
{
	getRange = sheet.get_Range("A1", Type.Missing);
}

foreach (Excel.Range range in getRange)
{

	string server = "YourSmtpServerHer";
	int port = 25;
	string authUser = "YourSmtpUsername";
	string authPass = "YourSmtpPass";
	string sendFrom = "send.from@email.com";
	string subject = "Subject Of Your Mail";
	string body = "put the body of your mail here";
	string attachmentPath = @"c:\path\to\your\file\here.txt";

	MailMessage mail = new MailMessage();
	SmtpClient SmtpServer = new SmtpClient(server, port);
	mail.From = new System.Net.Mail.MailAddress(sendFrom);
	mail.To.Add(range.Text);
	mail.Subject = subject;
	mail.Body = body;

	System.Net.Mail.Attachment attachment;
	attachment = new System.Net.Mail.Attachment(attachmentPath);
	mail.Attachments.Add(attachment);

	SmtpServer.Port = port;
	SmtpServer.Credentials = new System.Net.NetworkCredential(authUser, authPass);
	SmtpServer.EnableSsl = true;



	SmtpServer.Send(mail);
}
 
Share this answer
 
Comments
Naveen Kumar Kaushik 13-Jan-13 6:21am    
Thanks for the code but i had already tried this one but i didn't get my desire output what i wanted. but any way thanks for your code & support for me......
David_Wimbley 13-Jan-13 11:26am    
Can you not dump the excel spreadsheet into some form of database? anything? sql server, mysql something?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900