如何在asp.net中发送确认邮件,无法获取IIS提取目录错误

时间:2012-07-20 12:03:31

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

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

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

提前致谢。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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>

有效......:)