0

私のアプリケーションでは、ライブ/ホットメールの電子メールを送信するコードがありますが、gmail では機能しません。電子メールの送信に使用されているアカウントを確認するためにチェックを作成しようとしましたが、機能していません。 Gmailメールを送信しようとすると. チェックに使用するコードは次のとおりです。

MailMessage msg = new MailMessage();
            msg.To.Add(txtAan.Text);
            msg.From = new MailAddress(txtGebruikersnaam.Text);
            msg.Subject = txtOnderwerp.Text;
            msg.Body = txtBericht.Text;

            string smtpcheck = txtGebruikersnaam.Text;
            smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));

            SmtpClient smtp = new SmtpClient();
            if (smtpcheck.ToLower() == "@gmail.com")
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 25;
            }
            else if(smtpcheck.ToLower() != "@gmail.com")
            {
                smtp.Host = "smtp.live.com";
                smtp.Port = 587;
            }
            smtp.EnableSsl = true;
            smtp.Credentials = new NetworkCredential(txtGebruikersnaam.Text, txtWachtwoord.Text);
            smtp.Send(msg);

Gmail を使用して電子メールを送信しようとすると、このコードでエラーが発生します。誰かこの問題について少し助けてもらえますか? はい、ポートも試しました:gmailの場合は465と587なので、それも問題ではないと思います。

4

2 に答える 2

4

この行は smtpcheck の値を変更しません

  smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));

あなたは書く必要があります

  smtpcheck = smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));

その結果、if 条件が失敗し、常に live.com でメールを送信するようになります。

編集: gmail の場合、このコードは動作することが確認されています

 SmtpClient sc = new SmtpClient("smtp.gmail.com");
 NetworkCredential nc = new NetworkCredential("username", "password");
 sc.UseDefaultCredentials = false;
 sc.Credentials = nc;
 sc.EnableSsl = true;
 sc.Port = 587;
于 2012-12-10T18:16:16.540 に答える
0
try
{
     MailMessage mail = new MailMessage();
     SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

     mail.From = new MailAddress("your_email_address@gmail.com");
     mail.To.Add("to_address");
     mail.Subject = "Test Mail";
     mail.Body = "This is for testing SMTP mail from GMAIL";

     SmtpServer.Port = 587;
     SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
     SmtpServer.EnableSsl = true;

     SmtpServer.Send(mail);
     MessageBox.Show("mail Send");
}
catch (Exception ex)
{
     MessageBox.Show(ex.ToString());
}
于 2012-12-10T18:17:40.230 に答える