开发者

ASP.NET quick contact form not working

开发者 https://www.devze.com 2023-02-09 14:43 出处:网络
I am having a Quick contact form on an aspx page which is not working I am using a try/catch block in case of an exception well till now only the catch is working.Please help me with the code I am usi

I am having a Quick contact form on an aspx page which is not working I am using a try/catch block in case of an exception well till now only the catch is working.Please help me with the code I am using the mail message class of the System.Net.Mail namespace.Her's the code

try
{
    MailMessage EmailMsg = new MailMessage();
    EmailMsg.From = new MailAddress(txtEmail.Text,txtName.Text);
    EmailMsg.To.Add(new MailAddress("mail address"));
    EmailMsg.Subject = "WebAssist Email form";
    EmailMsg.Body = "<html><body>This is a text<hr>Test test</body></html>";
    EmailMsg.IsBodyHtml = tru开发者_运维问答e;
    EmailMsg.Priority = MailPriority.Normal;
    SmtpClient MailClient = new SmtpClient("");
    MailClient.EnableSsl = true;
    MailClient.Credentials = new System.Net.NetworkCredential("","");
    MailClient.Send(EmailMsg);
    Label1.Visible = true;
    Label1.Text = "Email sent";
}
catch
{
    Label1.Visible = true;
    Label1.Text = "Your email was not sent";
}

Please tell what to write in the smtp constructor,in the network credentials Thank you in advance


Take the catch statement out - you want to let the exception propagate up the call stack so that you can find out what the problem is. All you're doing now is hiding it.

Once you've solved the problem I suggest you take a look at exception handling best practices.


The constructor for the SmtpClient class can be called in one of three ways:

  1. SmtpClient() - Initializes a new instance of the SmtpClient class by using configuration file settings.
  2. SmtpClient(String) - Initializes a new instance of the SmtpClient class that sends e-mail by using the specified SMTP server.
  3. SmtpClient(String, Int32) - Initializes a new instance of the SmtpClient class that sends e-mail by using the specified SMTP server and port.

As for what actual values need to be supplied, that's really between you and your SMTP server. We can't help you there. The string will be the name of the server (possibly even just its IP address) and the port will most likely be 25, but could be anything.

0

精彩评论

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

关注公众号