danny
danny

Reputation: 95

Emails not sent due to SMTP Exception

I have a general mailbox that could be used to send emails from my website. It has stopped working and throws an exception. The following is a screenshot of the error.

error http://img7.imageshack.us/img7/7227/42097647.png

The code is

  protected void Submit_Click(object sender, EventArgs e)
   {
    if (ValidateBox.Text == "6")
    {
        MailMessage message = new MailMessage();
        message.From = new MailAddress(EmailBox.Text.ToString());
        message.To.Add(new MailAddress("[email protected]"));
        message.Subject = "Message via CAM Website General Mailbox";
        string body = "Name: " + NameBox.Text.ToString() + Environment.NewLine + Environment.NewLine +
                      "Home Tel: " + HomeTelBox.Text.ToString() + Environment.NewLine + Environment.NewLine +
                      "Work Tel: " + WorkTelBox.Text.ToString() + Environment.NewLine + Environment.NewLine +
                      "Comment/Question: " + CommentBox.Text.ToString();

        message.Body = body;


        SmtpClient client = new SmtpClient();
        client.Send(message);

        Response.Redirect("~/Thank-You.aspx");
    }

    else
    {
        Label1.Visible = true;
    }



}

Could this be an error from the SMTP Client mentioned in my webconfig, How can I check if its working fine ? Thanks.

Upvotes: 2

Views: 161

Answers (2)

Dave
Dave

Reputation: 8451

You are not setting up the Network Credentials. In other words, you're not authenticating the email.

NetworkCredential myCredentials = new NetworkCredential(
"[email protected]","myPassword","mail.myDomainName.com");


client.Credentials = myCredentials;

Upvotes: 1

Heinzi
Heinzi

Reputation: 172200

This is a configuration issue.

The SMTP server specified in your web.config (system.net > mailSettings > smtp) is not configured for relaying. You have two options to fix this:

  1. Configure the SMTP server to allow relaying for the IP address of your web server (only!).
  2. Send SMTP credentials with SmtpClient (see Dave's answer for that).

Upvotes: 1

Related Questions