Task - A contact form in ASP.NET
Using an existing site, or creating a new empty website in ASP.NET, add or modify an existing 'contact' page to include the facility to send an email.
Your form should collect the following fields: Name, email address, subject and message body. You can set the 'Mode' property of the TextBox control to 'Multiline' which will create a text area for the body of the message.
Add a 'send' button, give it an appropriate id, such as btnSendEmail.
Before you can write the code to send the email, you will need to identify an email provider that permits the sending of emails using a secure SMTP server (e.g. rather than solely a web interface). Gmail is one such provider. Ensure you have an account with the provider and locate the settings (typically you will need to know the address of the host server, the port number, and whether or not the connection should be secure).
- Some providers, including Gmail, require you to log into the settings and allow access from less secure apps, in order to be able to send emails - check, and adjust this if necessary
You should also be aware that the account's username and password will be visible in the source code, so it's better not to use a personal account for this process. Note that the sending of mail using an insecure server (port 25) is often blocked by organisational firewalls.
Double click on the send button, this will create a method for your to write the code to send the message.
Create a new instance of the SmtpClient class using the following code:
SmtpClient smtpClient = new SmtpClient();
You will notice that SmtpClient is underlined. This is because the system does not know what an SmtpClient is. With the cursor in the text, press Ctrl + . (Control and full stop) and choose Resolve - using System.Net.Mail. (This adds a
using statement at the top of the code which informs the system where the information about the SmtpClient class can be found)
Next you should set the appropriate properties of the
smtpClient followed by a full stop will prompt you with a list of settings that can be configured. You should set the ones that seem appropriate based on the settings you found earlier.
Assuming you need to set credentials, you can create a set of credentials using the following code, replacing the username and password as necessary:
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("userName", "password");
- Set the credentials property of the
smtpClient to the credentials you created above
Next create a MailMessage, using the following code as an example, replacing the email addresses as appropriate:
MailMessage msg = new MailMessage("firstname.lastname@example.org", "email@example.com");
In the same way you added the properties for the SMTP client, set the body and subject fields for the
Keep in mind that whilst you are collecting the email address of the person using the form, it will not be their email address that is sending the email. Therefore you should find a way of appending this information to the subject or body of the message.
Finally add the following line of code to send the message
- Test the contact form, filling in each of the name, email, subject and message fields of the form with unique data, and check that data from each of those fields is visible in the email message received.
You can find a sample project on github, but you will need to modify the code to use a pre-existing Gmail account
- Once the message has been sent, hide the fields for sending the message, and instead display a success message.
- Send a different email to the person filling in the form, informing them that their message has been sent
- Add a drop-down list to the form so the user can specify the topic of the email, rather than typing a subject (examples might include 'Product query' or 'I want to return an item')
- Create a 'reply to' address, so that the recipient of the email can reply to the original sender
- On occasion, an email server may be unavailable. Add some error checking to handle the potential, and notify the user of failure if it occurs. Test the error handling works by temporarily changing the configuration so it cannot correct (i.e. a wrong password). Hint: you can use a
catch block to implement this.
- Add functionality to enable a user to attach a file to the message.
- To ensure that the email address is not mis-typed by the user, add a field for them to confirm it and check the data entered matches.
- The email form can currently be left empty, ensure that the fields cannot be left empty, and that a correctly formatted email address has to be input. You will need to implement some validation to achieve this.