DiH DiH - 3 months ago 11
C# Question

Send Yahoo email to more than one address

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" ;

Answer
    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);
         }
}