Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying with below code but it sends a single mail at a time.But i want to split the textbox for sending multiple emails at a single click......

C#
private void btnsendnow_Click(object sender, EventArgs e)
{
    //email_send
    try
    {
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true;
        client.Timeout = 100000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("EMAIL-ID", "PASSWORD");
        MailMessage msg = new MailMessage();
        msg.To.Add(txtto.Text);
        msg.From = new MailAddress("aarnapraj@gmail.com");
        msg.Subject = txtsubject.Text;
        msg.Body = txtmessage.Text;
        Attachment data = new Attachment(txtattachment.Text);
        msg.Attachments.Add(data);
        client.Send(msg);
                
        // List<string> mail = txtto.Text.Trim().Split(',').ToList();
        //client.Send(msg,mail);
        MessageBox.Show("successfully send.....");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Posted
Updated 29-Jan-15 23:38pm
v2
Comments
_Asif_ 30-Jan-15 5:54am    
Please elaborate more

Well, presuming the thing separating the lines is a CRLF (\r\n), try this:

Replace:
msg.To.Add(txtto.Text);

With:
foreach (string address in txtto.Text.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
{
   msg.To.Add(address); 
}


[EDIT - Fixed incorrect split]
 
Share this answer
 
v2
You need to use Split to split the text box text and then have a loop though each line - i.e. each address.

The following assumes that you have put a carriage-return-linefeed between each address (i.e. have hit the Enter button between them)

C#
var x = txtto.Text.Split(Environment.NewLine[0]);
foreach (var s in x)
    Console.WriteLine("|{0}|", s.Trim());

Note the use of [0] at the end of NewLine - this is because I need to split on a char not a string. This also explains the .Trim() used when I'm displaying the results ... NewLine is CarriageReturn + LineFeed.

Also note the use of Environment.NewLine instead of hard-coding ascii values for CRLF

Edit - a slightly neater solution
C#
var x = textBox1.Text.Split(new [] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
foreach (var s in x)
    Console.WriteLine("|{0}|", s);
 
Share this answer
 
v2
Comments
Rob Philpott 30-Jan-15 5:54am    
Ah!, great minds think alike, although I prefer your use of Environment.NewLine. I think Split accepts a char[], and I think string has an implicit cast so doubt you need to do the [0]/trim part.
CHill60 30-Jan-15 5:56am    
When I tried it I was getting extra lines. I'm about to offer an alternative too. Your solution appears to be missing a parameter - I'm getting compile errors in VS 2010 Express?
Rob Philpott 30-Jan-15 5:58am    
Hmm. I need to dig out Visual Studio now...
Rob Philpott 30-Jan-15 6:03am    
You're right! No implicit cast (but you can use .ToArray()), but then its splits on either /r or /n hence the blank lines.

I'm sure I've split string on more than just characters before. Need to have a think..
CHill60 30-Jan-15 6:05am    
That was my first stab at it too ... we're suffering from the same vague memory!! :)

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