使用C#在ASP.NET中发送电子邮件错误

时间:2010-09-27 07:58:29

标签: c# asp.net email

这是电子邮件发送的代码,但它在try阻止:

中出错
protected void Page_Load(object sender, EventArgs e)
{
    EmailUtility email = new EmailUtility();
    email.Email = new MailMessage();
    string body = email.GetEmailTemplate(Server.MapPath("~/EmailTemplates"), "test.htm");
    EmailMessageToken token = new EmailMessageToken();
    token.TokenName = "$Name$";
    token.TokenValue = "Ricky";
    EmailMessageTokens tokens = new EmailMessageTokens();
    tokens.Add(token);
    //av.LinkedResources.Add(lr);
    email.Email.Body = email.ReplaceTokens(body, tokens);
    email.Email.To.Add(new MailAddress("sahil4659@gmail.com"));
    email.Email.IsBodyHtml = true;
    email.Email.From = new MailAddress("sahil4659@gmail.com");
    email.Email.Subject = "Hello from bootcamp";
    email.SMTP.Host = ConfigurationManager.AppSettings["SMTPServer"];
    try
    {
        email.SMTP.Send(email.Email);
        Response.Write("Email sent !");
    }
    catch (Exception ex)
    {
        Response.Write(ex.StackTrace);
    }
}

错误是:

at System.Net.Mail.IisPickupDirectory.GetPickupDirectory()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at _Default.Page_Load(Object sender, EventArgs e) in c:\Users\Sahil\Desktop\Csharp Email Code(2)\Test website\EmailTest.aspx.cs:line 38

1 个答案:

答案 0 :(得分:0)

我会读你的想法并假设你看到System.Net.Mail.SmtpException: Cannot get IIS pickup directory.谷歌搜索,我看到你在网络配置中需要的SMTP配置参考。您的邮件服务器可能需要凭据,因此您可能需要以下内容:

<system.net>
    <mailSettings>
      <smtp from="fromaddress">
        <network defaultCredentials="true"
               host="smtpservername" port="smtpserverport" 
               userName="username" password="password"  />
      </smtp>
    </mailSettings>
</system.net>

...在你的web.config中。实质上,配置您的Web应用程序以便能够与SMTP服务器通信。

请参阅此主题以获取更多referencehere's relevant MSDN documentation