开发者

Email sending error in ASP.NET using C#

开发者 https://www.devze.com 2023-01-17 22:34 出处:网络
This is the code for email sending, but it gives error in try block: protected 开发者_如何学Cvoid Page_Load(object sender, EventArgs e)

This is the code for email sending, but it gives error in try block:

protected 开发者_如何学Cvoid 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);
    }
}

The error is:

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


I'm going to read your mind and assume that you're seeing System.Net.Mail.SmtpException: Cannot get IIS pickup directory. Googling around, I see references to SMTP configuration you'll need in your web configuration. Your mail server may require credentials, so you might need something like this:

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

... in your web.config. Essentially, configure your web app to be able to talk with the SMTP server.

See this thread for more reference, and here's relevant MSDN documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消