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

为什么不使用 GMAIL 作为 SMTP 服务器,因为您正在定义一个 GMAIL 帐户,将主机定义为“smtp.gmail.com”,端口为 587?

此处的具体细节:http: //support.google.com/mail/bin/answer.py ?hl=en&answer=13287

于 2012-07-20T13:09:19.643 回答
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 回答