【问题标题】:how to send a confirmation email in asp.net, cannot get IIS pickup directory error如何在 asp.net 中发送确认电子邮件,无法获取 IIS 拾取目录错误
【发布时间】:2012-07-22 16:27:08
【问题描述】:

我想在注册页面向用户发送确认电子邮件。以下代码是相关部分:

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 服务,但我看不到这个服务。此错误是否与该服务有关?

提前致谢。

【问题讨论】:

  • 您使用哪种电子邮件服务?它不应该在主机下吗?
  • 在调试时,我意识到主机等于“gmail.com”。你是这个意思吗?
  • 你在用gmail服务器发邮件吗??
  • 我不知道我使用哪个电子邮件服务。我只想从一个 gmail 地址向另一个 gmail 地址发送一封电子邮件。你能告诉我如何在代码中显示电子邮件服务吗?
  • 我已经在这里回答了这个问题stackoverflow.com/questions/7982810/…希望这会有所帮助

标签: asp.net email iis smtp confirmation-email


【解决方案1】:

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

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

【讨论】:

  • 我设置为 sc.host="smtp.gmail.com" 和 sc.port=587 但它没有改变,我在调试时已经意识到。当它下降到捕获时,它给出了这个错误:无法获取 IIS 拾取目录
  • 删除这一行:sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;然后再试一次。
  • 我已经删除了这部分并尝试了,但这次它给出了这个错误: SmtpException was catched : The SMTP server requires a secure connection or the client was not authenticated.服务器响应是: 5.7.0 必须首先发出 STARTTLS 命令。 o2sm15359902wiz.11
  • 在 SmtpClient 上,设置 EnableSsl = true;通过msdn.microsoft.com/en-us/library/…
  • 我添加了 sc.EnableSsl = true;并删除 sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;但有一个错误: SmtpException was catched : SSL must not be enabled for pick-directory delivery methods。我
【解决方案2】:

感谢您的帮助。 我已经解决了这个问题。我已将代码更改为以下代码:

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>

行得通.. :)

【讨论】:

    猜你喜欢
    • 2011-01-12
    • 2015-08-03
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2018-11-28
    • 2020-04-28
    相关资源
    最近更新 更多