我想在注册页面向用户发送确认电子邮件。以下代码是相关部分:
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 服务,但我看不到这个服务。此错误与该服务有关吗?
提前致谢。