0

登録ページでユーザーに確認メールを送信したい。次のコードは関連部分です。

try
{
    SmtpClient sc = new SmtpClient();
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    string Ema = u.UserMail.ToString();
    MailAddress gonderen = new MailAddress("admin@gmail.com", "Hello");
    sc.Host = "smtp.gmail.com";
    sc.Port = 587;
    mail.To.Add(Ema);
    mail.Subject = "Confirmation Message";
    mail.From = gonderen;
    mail.IsBodyHtml = true;
    mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
    mail.Body = "<html><body>";
    sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
    sc.Send(mail);
    MESSAGE(true, "Sending mail is successful");
}
catch (Exception)
{
    MESSAGE(true, "Sending mail is unsuccessful!");
}

ただし、関連するユーザーにメールは送信されません。フォーラムを見て、web.config に次の部分を追加しました。

  <system.net>
    <mailSettings>
      <smtp from="myaddress@gmail.com ">
        <network host="smtp.gmail.com" defaultCredentials="false"
      port="587" userName ="myaddress@gmail.com" password="password" />
      </smtp>
    </mailSettings>
  </system.net>

しかし、何も変わりませんでした。それから私はデバッグし、それはtryステートメントに入り、それが来るとsc.Send(mail);キャッチに落ちます。私の間違いはどこですか?

さらに、デバッグ中に、次のエラーが表示されることに気付きました: cannot get IIS pickup directory. サービスから smtp サービスがあるかどうかを制御しましたが、このサービスを表示できませんでした。このエラーはそのサービスに関連していますか?

前もって感謝します。

4

2 に答える 2

0

助けてくれてありがとう。私はこの問題を解決しました。コードを次のように変更しました。

try
{ 
    SmtpClient sc = new SmtpClient();
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    string Ema = u.UserMail.ToString();
    MailAddress gonderen = new MailAddress("admin@gmail.com", "Hello");
    sc.Host = "smtp.gmail.com";
    sc.Port = 587;
    sc.EnableSsl = true;
    mail.To.Add(Ema);
    mail.Subject = "Confirmation Message";
    mail.From = gonderen;
    mail.IsBodyHtml = true;
    mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
    mail.Body = "<html><body>";
    mail.Body += "</body></html>";
    sc.DeliveryMethod = SmtpDeliveryMethod.Network;
    sc.Send(mail);
    MESAJ(true, "Sending mail is successful");
}
catch (Exception)
{
    MESAJ(true, "Sending mail is unsuccessful!");
}

そして、次のようにweb.configを設定しました:

<mailSettings>
  <smtp deliveryMethod="Network"
              from="admin@gmail.com">
    <network defaultCredentials="false" host="smtp.gmail.com" port="587"
              userName="admin@gmail.com"  password="password" />
  </smtp>
</mailSettings>

それは動作します.. :)

于 2012-07-22T08:27:08.087 に答える
0

ホストを「smtp.gmail.com」、ポートを 587 と定義して GMAIL アカウントを定義しているため、GMAIL を SMTP サーバーとして使用しないのはなぜですか?

詳細はこちら: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

于 2012-07-20T13:09:19.643 に答える