2016-08-31 9 views
-2

私はこのコードを使用してyahooからメールを送信しています。複数のアドレスにYahoo!メールを送信

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

     string emailFrom = "[email protected]"; 
     string password = "xxxxxx!"; 
     string emailTo = "[email protected]"; 
     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); 

メールアドレスを追加したい場合はどうすればよいですか?私はこれを試してみましたが、私はエラーを取得:

string emailTo = "[email protected]" ,"[email protected]" ; 
+1

これを試してみてください: '' 'string emailTo =" [email protected]、[email protected] ";' '' '(すべての電子メールアドレスは" " - つまり" ") – scana

答えて

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

    string emailFrom = "[email protected]"; 
    string password = "xxxxxx!"; 
    List<string> emailToList = new List<string>; 
    emailToList.Add("[email protected]"); 
    //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); 
     } 

または

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

    string emailFrom = "[email protected]"; 
    string password = "xxxxxx!"; 
    List<string> emailToList = new List<string>; 
    emailToList.Add("[email protected]"); 
    //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); 
     } 
} 
1

私はあなたがして、コンマとスペースで区切られた複数の電子メールアドレスを持つ1つの文字列を、持っていると仮定しています、例えば

string emailTo = "[email protected], [email protected], [email protected]"; 

emailToを文字列の集合に分割する必要があります。これを実現するには、次のように使用することができます。

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

この方法の利点は、あなたが手動メールアドレスを解析し、あなたが持っているイベントの中で自分自身をリストの受信者にそれらを追加する必要はありませんということですemailTo文字列には多くの電子メールアドレスが含まれています。

+0

この方法で電子メールを送信する理由は、スパムとしてHotmailに送信しますか? – DiH

+0

おそらく、提示された方法とは関係ありません。テスト中に同じメッセージを複数回送信してしまった可能性もあります。これはしばしば迷惑メールとして取り上げられます。送信者をホワイトリストに登録することをお勧めします。 – arbitrarystringofletters

+0

そうであることを確認したい場合は、もう一方の方法を使用して、迷惑メールかどうかを確認してください。そうであれば、正確に同じメッセージを複数回送信しているからです。そうでなければ、私の方法には本当に何か問題があります。 – arbitrarystringofletters

関連する問題