Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

The main scope of my task to create the email functionality to send the e-mail to every each user separately but it sends to all users(all user names are display in To property. Having a problem to accomplish this.

Here's the fragment on the code:

C#
ArrayList list_emails = new ArrayList();
    int i = 0;
    string email  = null;
..................................
db Conncetion; datareader; call stored proc;
..................................

ReaderEL = custTable_db.ExecuteReader(dbCommand);
//Loop through reader and add settlement details to arraylist
while (ReaderEL.Read())
{
    list_emails.Add(ReaderEL.GetValue(0).ToString());
    i = i + 1;
}

       MailMessage objMail = new MailMessage();
       SmtpClient mSmtpClient = new SmtpClient();

foreach (string email_to in list_emails)                      
       {
                                                                  
         objMail.From = new MailAddress("noreply@abc.com");
         objMail.To.Add(new MailAddress(email_to));
         objMail.Subject = txtSubject.Text.Trim();
         objMail.Body = ftbEmailMesg.Text;
         objMail.Priority = MailPriority.Normal;
         objMail.IsBodyHtml = true;
         mSmtpClient.Host = "00.000.0.000";
         mSmtpClient.Send(objMail);              
        }

Appreciate any help!
Posted

What's would make the difference if you send just one message to a list of addresses? Only one: all the recipients would be aware of all other recipients.

So, I can assume this is the reason you might have for sending it "to every each user separately", to keep other users in secret. I cannot see other reasons.

If so, you have two options 1) put just one address to the "To:" header and other addressed to "BCC:" header, which makes the content of this list to be sent to each participant; 2) send a list of separate messages in a loop, changing only the "To:" header, with only one address on each iteration (there is no such thing as miracle :-)).

—SA
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 10-Oct-14 16:01pm    
I have editted your answer, 'the' had to be 'to'.
Sergey Alexandrovich Kryukov 10-Oct-14 16:14pm    
Thank you very much, Afzaal.
—SA
Member 9901144 20-Oct-14 16:42pm    
Thank you for your respond!!
Here's the fragment of the code for your suggested option 2:
objMail.From = new MailAddress("noreply@abc.com");
objMail.Subject = txtSubject.Text.Trim();
//objMail.Bcc.Add(bcc);
objMail.Body = ftbEmailMesg.Text;
objMail.Priority = MailPriority.Normal;
objMail.IsBodyHtml = true;

for (i = 0; i < list_Emails.Count; i++)
{
email = Convert.ToString(list_Emails[i]);
objMail.To.Add(email);
// objMail.Bcc.Add(email);
mSmtpClient.Host = "00.000.0.000";
mSmtpClient.Send(objMail);
}
When I used this loop to send a separate email to the user, unfortunately, all users email display in "To" property. Could be my loop is wrong?
Thanks Sergey!
Sergey Alexandrovich Kryukov 20-Oct-14 16:53pm    
Where are all your addresses? You show only one...
—SA
Member 9901144 20-Oct-14 16:58pm    
list_Email is my ArrayList which hold the list of emails from database(see my orig code)
That is why you need to use the ASP.NET way of doing this, as already mentioned you can use a loop, to send it to all of the recipients, the only single message. For example

C#
foreach (var recipient in recipients) {
  // send the email
}


This is an example code which can be implemented as follows,

C#
// example array of recipients 
var recipients = {"array@example.com", "ofrecipients@example.com"};
// now loop through the recipients, 
foreach (var recipient in recipients) {
  // use the WebMail class, 
  WebMail.Send(to: recipient,
               subject: "Subject of the Email",
               body: "The message of the email."
               );
}


Once this ends, the emails would be send to the users, in the list or array that you will pass to the loop. In the loop, to field is a variable and depends on the value passed by the loop.

You can learn how to send emails using ASP.NET WebMail class from an article I wrote back a few months, Sending Emails Easily Using ASP.NET Helpers[^]

Alternate way

There is another way, of doing this, that is to attach a BCC (Blind Carbon Copy) to the email this would not reveal the recipient's information to other recipients, but will not be a good way, since you don't know how many email recipients you might have and so on. So using a loop and passing values of email address of the recipients will be a better bid for this.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Oct-14 16:15pm    
Some more detail; a 5.
—SA
Afzaal Ahmad Zeeshan 10-Oct-14 16:17pm    
Thanks for appreciating my answer sir, :-)
Member 9901144 10-Oct-14 16:59pm    
Appreciated your quick respond!
The .net app(3.5)is using System.Net.Mail class....would prefer to use this class :)
Afzaal Ahmad Zeeshan 11-Oct-14 8:59am    
Ohkie then, you can try the remaining answer of mine. :-)

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