-3

I'm using this code to send an email from yahoo.

  string smtpAddress = "smtp.mail.yahoo.com";
        int portNumber = 587;
        bool enableSSL = true;

        string emailFrom = "mitshel@yahoo.com";
        string password = "xxxxxx!";
        string emailTo = "dimitris.chris@yahoo.com"; 
        string subject = "Hello";
        string body = "Hello, I'm just writing this to say Hi!";

        using (MailMessage mail = new MailMessage())
        {
            mail.From = new MailAddress(emailFrom);
            mail.To.Add(emailTo);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = true;
            // Can set to false, if you are sending pure text.


            using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
            {
                smtp.Credentials = new NetworkCredential(emailFrom, password);
                smtp.EnableSsl = enableSSL;
                smtp.Send(mail);

But what if I want to add more email addresses? I tried this, but I get an error:

 string emailTo = "mitsoshellas@yahoo.com" ,"dimitris.christoforidis@hotmail.com" ;
DiH
  • 451
  • 2
  • 8
  • 18
  • 1
    Try this: ````string emailTo = "mitsoshellas@yahoo.com, dimitris.christoforidis@hotmail.com";```` (all of the e-mail addresses are contained as a one string, within "" - i.e. "") – scana Aug 31 '16 at 17:30

2 Answers2

1
    string smtpAddress = "smtp.mail.yahoo.com";
    int portNumber = 587;
    bool enableSSL = true;

    string emailFrom = "mitshel@yahoo.com";
    string password = "xxxxxx!";
    List<string> emailToList = new List<string>;
    emailToList.Add("dimitris.chris@yahoo.com");
    //add as many other as you like 
    string subject = "Hello";
    string body = "Hello, I'm just writing this to say Hi!";

    using (MailMessage mail = new MailMessage())
    {
        mail.From = new MailAddress(emailFrom);
        foreach(string recipient in emailToList){
            mail.To.Add(recipient);
        }
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        // Can set to false, if you are sending pure text.


        using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
        {
            smtp.Credentials = new NetworkCredential(emailFrom, password);
            smtp.EnableSsl = enableSSL;
            smtp.Send(mail);
         }

or

    string smtpAddress = "smtp.mail.yahoo.com";
    int portNumber = 587;
    bool enableSSL = true;

    string emailFrom = "mitshel@yahoo.com";
    string password = "xxxxxx!";
    List<string> emailToList = new List<string>;
    emailToList.Add("dimitris.chris@yahoo.com");
    //add as many other as you like 
    string subject = "Hello";
    string body = "Hello, I'm just writing this to say Hi!";
    foreach(string recipient in emailToList){
    using (MailMessage mail = new MailMessage())
    {
        mail.From = new MailAddress(emailFrom);
        mail.To.Add(recipient);

        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        // Can set to false, if you are sending pure text.


        using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
        {
            smtp.Credentials = new NetworkCredential(emailFrom, password);
            smtp.EnableSsl = enableSSL;
            smtp.Send(mail);
         }
}
Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21
1

I'm assuming you have one string with multiple email addresses, delimited by a comma and then a space, e.g.

string emailTo = "someEmail@email.com, someEmail2@email.com, someEmail3@email.com";

You need to separate emailTo into a collection of strings. To accomplish this, you can use the following:

// Separate the emailTo string into a list of email addresses
List<string> emailAddresses = new List<string>();

    int startingIndex = 0;
    while (startingIndex < emailTo.Length)
    {
        int commaIndex = emailTo.IndexOf(",", i);
        if (commaIndex != -1)
        {
            //Extract the email address
            string emailAddress = emailTo.Substring(startingIndex, commaIndex - startingIndex);

            // Remove the space following the comma
            if (emailAddress.Substring(0, 1) == " ")
            {
                emailAddress = emailAddress.Substring(1, emailAddress.Length - 1);
            }
            i = startingIndex + 1;
            result.Add(emaiAddress);
        }
    }

// Add each email address to the message's recipients
foreach(string emailAddress in emailAddresses)
{
    mail.To.Add(emailAddress);
}

The benefit of this method is that you don't have to manually parse email addresses and add them to the recipients list yourself in the event that you have one emailTo string that contains many email addresses.

  • Why sending email by this way, send it to hotmail as spam? – DiH Aug 31 '16 at 15:55
  • It's probably not related to the method presented. It could be the fact that you've sent the same message multiple times during your testing. This often gets picked up as spam. I'd suggest whitelisting the sender. –  Aug 31 '16 at 16:04
  • If you want to confirm that that's the case, try using the other method and see if it goes to spam. If it does, then it's because you're sending the exact same message multiple times. If it doesn't, then there is indeed something wrong with my method. –  Aug 31 '16 at 16:05
  • Thank you for your help i will check it.Also can yahoo block my email if i send bulk emails by this way? – DiH Aug 31 '16 at 16:09