As a busy Designer, I tend to leave forms and server side code to Developers, but even small sites normally require a contact us form.
In the old days, one would put a mailto: link to your email address. However, this will normally lead to spam bots getting hold of the email and spamming it like crazy, and/or it will launch Outlook (or some other email application you have set as default) which isn't really a great thing as lot's of people use Hotmail, Yahoo or some other Web Mail instead.
The answer is normall an email form, but to successfully send this we need not only the form, but some sort of server side code to process it and deliver the message.
I am quite a fan of ASP.NET as my server supports it (as I share my server with Alex, a friend who is an ASP.NET Developer) and I like all the friendly "Designer" features that the Visual Studio IDE has to offer.
To use this form in our ASP.NET web site we use the following controls in our HTML contact page (contact.aspx):
<form method="post" action="">
<ul class="lstContact">
<li class="labelContainer">
<asp:Label ID="confirmMessage" runat="server" Text="Thank you, your message has been sent!" Visible="false">
</asp:Label></li>
<li><asp:textbox runat="server" id="Name" Text="name" /></li>
<li><asp:textbox runat="server" id="txtEmail" Text="email" /></li>
<li><asp:textbox runat="server" id="Subject" Text="subject" /></li>
<li><asp:textbox id="txtMessage" runat="server" Text="message" textmode="multiline" /></li>
<li><asp:button id="cmdSend" runat="server" /></li>
</ul>
</form>
Note, I have set the form out in a list which makes it easier to lay out, especially if we add labels which we can float left and float their corresponding input/textarea to the right.
We are going to show the label (confirmMessage) when the user clicks send and the form has finished sending the email. So to begin with, this label is set to Visible="false".
Because this is ASP.NET, we don't need an onclick event on the send button, we will attach an event handler in the C# code-behind file (contact.aspx.cs):
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.cmdSend.Click += new EventHandler(cmdSend_Click);
}
//Will be called when button has been clicked
public void cmdSend_Click(object sender, EventArgs e)
{
//Call send email function
Send(txtMessage.Text, txtEmail.Text, "youaddress@yourdomain.co.uk", "Email from />www.yourdomain.co.uk");
confirmMessage.Visible = true;
}
public static string Send(string Message, string EmailFrom, string EmailTo, string Subject)
{
System.Net.Mail.MailMessage objMessage = new System.Net.Mail.MailMessage(EmailFrom, EmailTo);
SmtpClient objClient = new SmtpClient();
objMessage.IsBodyHtml = true;
objMessage.Subject = "" + Subject;
objMessage.Body = "" + Message;
//This is the mail server ip
objClient.Host = "0.0.0.0";
try
{
objClient.Send(objMessage);
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
}
This is the complete code-behind file so you can see the namespaces used etc.
So, when the page loads we attach out event handler to watch out for the user clicking our Send button, which has the ID of "cmdSend". When the send button is clicked, the code sends the message and users email address to the specified email address and with the specified subject so you know it is a genuine email from your web site.
It also show the confirmation message label so the web site visitor knows the email has been sent.
It doesn't send if the email and message fields are left blank.
Hope this helps! Please note, I am only publishing relevant coments on this topic.