• 0

[ASP.NET/C#] sending mail and confirmation


Question

I have a contact form for people to send me email.

1. I tried it using my email but didn't receive anything.

2. I want a confirmation message to appear for the user when they send an email, how do i do that?

I created the contact form using textbox control and not the Wizard in VS 2008.

Here's the code i have in contact page.

protected void PageLoad(object sender, EventArgs e)
	{
		if (IsPostBack)
		{
			SmtpClient sc = new SmtpClient("smtp.DOMAIN.com");
			StringBuilder sb = new StringBuilder();
			MailMessage msg = null;

			sb.Append("Email from: " + txtEmail.Text + "\n");
			sb.Append("Message: " + txtMessage.Text + "\n");

			try
			{
				msg = new MailMessage(txtEmail.Text,
					"email here, i know", "Message from website",
					sb.ToString());

				sc.Send(msg);
			}
			catch (Exception ex)
			{
				//something bad happend
				Response.Write("Something bad happend!");
			}
			finally
			{
				if (msg != null)
				{
					msg.Dispose();
				}
			}
		}
	}

and added this to web.config

<system.net>
		<mailSettings>
			<smtp>
				<network host="smtp.DOMAIN.com"
						 password="password"
						 userName="username"/>
			</smtp>
		</mailSettings>
	</system.net>

1 answer to this question

Recommended Posts

  • 0

something like this:

try

{

msg = new MailMessage(txtEmail.Text,

"email here, i know", "Message from website",

sb.ToString());

sc.Send(msg);

Response.Write("Something good happend!");

}

catch (Exception ex)

{

//something bad happend

Response.Write("Something bad happend!");

}

finally

{

if (msg != null)

{

Response.Write("Nothing happend!");

msg.Dispose();

}

}

If you step through the code, odds are you'll get a stop on sc.Send(msg) telling you the smtp server connection failed, which is why you never got an email.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.