Task - A contact form in ASP.NET

  1. 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.
  2. 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.
  3. Add a 'send' button, give it an appropriate id, such as btnSendEmail.
  4. 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).
  5. 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
  6. 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.
  7. Double click on the send button, this will create a method for your to write the code to send the message.
  8. Create a new instance of the SmtpClient class using the following code:
    SmtpClient smtpClient = new SmtpClient();
  9. 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)
  10. Next you should set the appropriate properties of the smtpClient. Typing 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.
  11. 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"); 
  12. Set the credentials property of the smtpClient to the credentials you created above
  13. Next create a MailMessage, using the following code as an example, replacing the email addresses as appropriate:
    MailMessage msg = new MailMessage("sender@server.com", "recipient@server.co.uk"); 
  14. In the same way you added the properties for the SMTP client, set the body and subject fields for the msg object.
  15. 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.
  16. Finally add the following line of code to send the message
  17. 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

Further tasks

Advanced Tasks