Moldoveanu Marius
Moldoveanu Marius

Reputation: 35

Send multiple emails from listbox c#

hi im geting my email list from db in a listbox(email_c) and i need to send to all

this is the error that i get with my code System.FormatException: 'The specified string is not in the form required for an e-mail address.'

        login = new NetworkCredential(txtuser.Text, txtPassword.Text);
        client1 = new SmtpClient(txtSmtp.Text);
        client1.Port = Convert.ToInt32(txtPort.Text);
        client1.Credentials = login;

        string emails = email_c.GetItemText(email_c.Items);

        
            msg = new MailMessage { From = new MailAddress(txtuser.Text + txtSmtp.Text.Replace("smtp.", "@"), "Sesizare", Encoding.UTF8) };
            msg.To.Add(new MailAddress(emails));
       

            if (!string.IsNullOrEmpty(txtCC.Text))
                msg.To.Add(new MailAddress(txtCC.Text));
            msg.Subject = txtSubject.Text;
            msg.Body = "Abonamentul dumneavoastra a fost activat";


            msg.BodyEncoding = Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.Normal;
            msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            client1.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
            string userstate = "Ttimite...";
            client1.SendAsync(msg, userstate);

Upvotes: 0

Views: 89

Answers (1)

MarianM
MarianM

Reputation: 91

The MailAddress does represent the address of an electronic mail sender or recipient. That means it is one single email.

Because of that, instead of writing all the email string into a single string variable you should iterate over the items in the listbox and add each of them individually to the MailMessage. Something like this:

        foreach (var email in email_c.Items)
        {
            msg.To.Add(new MailAddress(email.ToString()));
        }        

Upvotes: 1

Related Questions