# VBForums CodeBank > CodeBank - ASP / ASP.NET / MVC / Web API >  [RESOLVED] A simple contact form asp.net 2.0

## met0555

This is a very basic contact form which can be used for any kind of website. Web form contains name, email, subject and message inputs. When visitor ckicks send button, script send all informatiom from contact form.  Change only mail server and default email within the script.




```
<%@ Page Language="VB" Debug="true" %>
<% @Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">

Sub Send2Mail (sender as Object, e as EventArgs)

Dim objMail as New MailMessage()

  objMail.To = "TAREGET_EMAIL@ADRESS"
  objMail.From = strEmail.Text

  objMail.BodyFormat = MailFormat.Text
  objMail.Priority = MailPriority.Normal
  objMail.Subject = strSubject.Text

  objMail.Body = "Name : " + strName.Text + vbNewLine + "Email : " + strEmail.text + vbnewLine + "Message : " + strYourMsg.text
   
  SmtpMail.SmtpServer = "mail.YOUR_SMTP_SERVER.com"
  SmtpMail.Send(objMail)


  strMessage.Visible = true

End Sub

</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to send email</title>
</head>
<body>

  <asp:panel id="strMessage" runat="server" Visible="False">
      Thanks for your kind message ...  </asp:panel>

    <form runat="server">
      <b>First Name:</b> <br/>
      <asp:textbox id="strName" runat="server" />
      <br><br>

      <b>Email Address:</b><br/>
      <asp:textbox id="strEmail" runat="server" />
       <br><br>

      <b>Subject:</b><br/>
      <asp:textbox id="strSubject" runat="server" />
       <br><br>

       <b>Your Message</b><br/>
      <asp:textbox id="strYourMsg" runat="server" Columns="45" Rows="10" TextMode="MultiLine" />
        <br />
      <asp:button runat="server" id="func" Text="Send Message"
                  OnClick="Send2Mail" />
    </form>
</body>
</html>
```

----------


## ehsanvar

Hi, Thanks for the codes, very simple and useful...

I have changed the email address and also set SMTP to local host...

when trying it without debugging and filling the required fields, after pressing the submit button, it says that the message has been sent... bet I don't receive it in my email.

where do you think the problem might be? 

Please help me... i'm really stuck here. 

Thanks again

----------


## met0555

Hi, 

Did you try your localhost if it's working with another script or program?

thanks

----------


## ehsanvar

well, to be honest i am very new to this subject... i haven't got any other script.. is that possible for you to provide me with a very simple script?

Thanks

----------


## gep13

Hey,

The problem here is likely to do with your SMTP configuration, not the code that has been provided.  Are you sure you have configured the necessary settings to allow relaying of messages through your SMTP server?

Gary

----------


## HowTo

can you illustrate step by step about configuring the necessary settings to allow relaying of messages through your SMTP server?

----------


## gep13

Hey,

This isn't really something that can be provided, as each SMTP server is different, and requires different authentication, etc.  Whose SMTP Server are you trying to use?  Your ISP?  They should be able to provide you with the information that you need.

Gary

----------


## tychaos

This form is nice, Thanks! 
I need to add radio buttons and checkboxes to the form, I tried a few times and failed, kept getting a runtime error. Big newbie here so if someone could help, I would think it would be easy but I must be missing a detail somewhere. 
Tim

----------


## gep13

Hello,

In order to help you we are going to need some more information.

Can you show the ASPX markup and code that you are using, as well as providing more information about the error that you are getting.

Gary

----------


## PokerMunkee

Thank you for posting this.  It works great.

Just one request...

Can you provide code to redirect to a different page?

----------


## PokerMunkee

nevermind, figured it out.

i put Response.Redirect("http://domain.com/page.html") below strmessage.visible.


Could you provide how to put the user's IP address at the bottom of the email?

----------


## met0555

vb Code:
Dim sClientIP As String = Request.ServerVariables("remote_addr")

This will get your client's IP info.

----------


## PokerMunkee

Thanks!

In case anyone does this, I added this to the end of objMail.Body so it shows up in the email:

+ vbnewLine + "IP Address : " + sClientIP

----------


## gep13

The only thing that I would add is that vbnewline is an "old" way of doing it, you might want to think about using Environment.NewLine instead.

Gary

----------


## edusa2000

HI, After submitting form, we get this <asp :Stick Out Tongue: anel id="strMessage" runat="server" Visible="False">
      Thanks for your kind message ...  </asp :Stick Out Tongue: anel>

but It will not reset fields. is there any way after you submit the form to state Thank you message ONLY without the forms fields? how do you go about that?

----------


## met0555

If you are using my example, u can do it this way

write this after this strMessage.Visible = true



vb Code:
strEmail.Visible = false   MailFormat.Visible = false  strSubject.Visible = false  strName.Visible = false strEmail.Visible = false

----------


## edusa2000

FYI, Congratulations! your sample is great. I love it. 

I added the code suggested and unfortunately my page brakes and i get page not found. if i remove the code, it works fine.
Thanks

----------


## met0555

Can you please post how u added the code.

thx

----------


## edusa2000

Test no test

----------


## met0555

ok, pls  try this if still not working pks indicate on which line is the error.


```
strSubject.Visible = false
strName.Visible = false
strEmail.Visible = false
strYourMsg.Visible = false
```

thx

----------


## edusa2000

Ok, I give up. Now it removes the text boxes, but the labels remain there. I am uploading the whole code

----------


## met0555

What i wud suggest, is that u redirect the user to a new page.

----------


## edusa2000

Hi, as you can see, i already did it and it worked OK, buti rather see it in the same window. So you think this is it?

----------


## met0555

Change the plain text html label and replace it with <asp:label id="lblName" runat="server" /> .... then switch the visibility status to off.

----------


## edusa2000

Nah! It didn't work. well tahnks anyways buddy. I appreciate it

----------


## gep13

Hello,

In order to best help you, we are going to need to see all the code that you are using.  Is it possible that you can upload a zip file with the code in it, along with an explanation of exactly what it is that you are trying to do, as well as a description of what exactly isn't working?

Gary

----------


## edusa2000

HI
Thank you. All i want is after i submit the form, to say thank you in the same page. I dont want to redirect.  Here is the file

----------


## gep13

In which case, all you need to do is to set the Text property of a Label control on the same page once your post back to the server is finished.

Gary

----------


## skyman2610

thanks for your code, i think it so simple and useful for me and most of peoples
can you share some code like it
thanks!!

----------

