Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get email addresses from DB

Status
Not open for further replies.

patrickstrijdonck

Programmer
Jan 18, 2009
97
NL
Hi all,

Im trying to mail a attachment from C# to multiple users, now im using this:

Code:
                mail.To.Add("email@internet.com");
                mail.To.Add("email2@internet.com");

What i want is to select the email addresses from a table.

the addresses are all in 1 record. so the table looks like this

tlbEmail:
Categorie - Email1 - Email2 - Email3 - Email4

Now i want to select all those 4 email addresses en put them in the Mail.To.Add

Any help on this is appriciated, im still looking with my friend google ;)
 
lets assume you already have a connection to the database
Code:
command.CommandText = "select [field] from [table] where id=@id";
var parameter = command.CreateParameter();
parameter.Name = "id";
parameter.Value = ...;
command.Parameter.Add(parameter);
var emailAddresses = command.ExecuteScalar() as string;

if(emailAddresses != null)
{
   foreach(var emailAddress in emailAddresses.Split(" - "))
   {
     mail.To(emailAddress);
   }
}
this is just sample code. In production there would also be error handling and the functionality
(selecting field, parsing email address, sending email) would be separated into different objects. but this should be you started.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason:
Does this code hold the previous entry
Code:
mail.To(emailAddress);

I would think only the last one would be actually be kept?
 
ralph, that code won't compile. it should be
Code:
mail.To.Add(emailAddress);

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top