Reputation: 1
I have created a form with a list box called lstCompany which displays search data, search textboxes are the "Name" and "PostCode" of company. I have a table called tblCompany which contains CompanyNo(primary key) with company details in the database including email. I would like to know how I can send email to the companies that I search on the list box. I have done the code which only sends email to one person who i put in code. Below is the code for send button
var fromAddress = new MailAddress("myemail", "name");
var toAddress = new MailAddress("other person's email", "name");
const string fromPassword = "password";
const string subject = "Testing";
const string body = "Testing";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
MessageBox.Show("Your email has been sent!");
}
}
Upvotes: 0
Views: 938
Reputation: 923
try something like this:
private MailMessage m = new MailMessage();
protected void ListTo_SelectedIndexChanged(object sender, EventArgs e)
{
m.To.Add(new MailAddress("[email protected]"));
}
If you your listbox manages multi selection, you may prefer:
protected void Button_Click(object sender, EventArgs e)
{
MailMessage m = new MailMessage();
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
m.To.Add(new MailAddress(item.Value));
}
}
Upvotes: 1