2016-07-23 11 views
0

で送信できません。電子メールの送信に使用するWindowsアプリケーションを作成しました。私は資格を与えました。 Google/settings/lesssecureアプリを有効にしました。それはを送信していません。そのエラーを示すSMTPサーバーが安全な接続を必要としているか、クライアントが認証されていません。サーバーの応答は:5.5.1認証が必要ですここに私のコードです。GmailメールはC#

MailMessage message = new MailMessage(); 
      message.From = new MailAddress("[email protected]"); 
      string[] mailaddress = new string[count]; 
      int i; 
      if (textSubject.Text != string.Empty) 
      { 
       message.Subject = textSubject.Text; 
       if (textBody.Text != string.Empty) 
       { 
        message.To="[email protected]" 
        message.IsBodyHtml = true; 
        string tmpBody = "Hello " + "," + "<br/> <br/>" + textBody.Text + "<br/> <br/>" + "Thanks and Regardds"; 
        message.Body = tmpBody; 
        SmtpClient client = new SmtpClient(); 
        client.UseDefaultCredentials = true; 
        client.Host = "smtp.gmail.com"; 
        client.Port = 587; 
        client.UseDefaultCredentials = false; 
        client.Credentials = new NetworkCredential("[email protected]", "mypassword"); 
        message.Priority = MailPriority.High; 
        client.EnableSsl = true;    
        client.Send(message); 
        MessageBox.Show("Mail has sent successfully !!!", "Success !"); 
       } 
       else 
       { 
        MessageBox.Show("Please Enter Body of the Message !"); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Please Enter Subject !"); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Failure !"); 
      log.Fatal(ex.Message); 
     } 
    } 
+1

可能な複製(http://stackoverflow.com/questions/20906077/gmail-error-the-:あなたは、コードの多くを必要としない、このコードは、添付ファイルなしで電子メールを送信するのに十分ですsmtp-server-requires-a-secure-connection-or-the-client-was-not) – Berkay

+0

あなたはその**メッセージを行うことはできません。= "[email protected]"へ** "To"プロパティは読み取り専用電子メールのMailAddressCollection。試してみてください** message.To.Add( "[email protected]"); ** – derloopkat

答えて

1

2段階検証をオンにした場合は、アプリケーション固有のパスワードを使用してログインする必要があります。ここに作成することができます: https://support.google.com/accounts/answer/185833?hl=en。通常のパスワードを使用する場合、例外5.5.1認証が必要です。 [この]の

const string from = "[email protected]"; 
    const string to = "[email protected]"; 
    const string subject = "This is subject"; 
    const string body = "This is body"; 
    const string appSpecificPassword = "akdfkajsdhklakdfh"; 

    var mailMessage = new MailMessage(from, to, subject, body); 
    using (var smtpClient = new SmtpClient("smtp.gmail.com", 587)) 
    { 
    smtpClient.EnableSsl = true; 
    smtpClient.Credentials = new NetworkCredential(from, appSpecificPassword); 
    smtpClient.Send(mailMessage); 
    } 
+0

私の問題の完璧な解決に感謝 –

関連する問題