WEB460 Lab 6 of 7: Sending E-mail and Testing Your Application

$ 12

WEB460 Lab 6 of 7: Sending E-mail and Testing Your Application

Lab Overview
This lab will not work if your Gmail, Yahoo, Outlook, or other email account is set up with two-factor authentication. You should use or create an account that does not have two-factor authentication to fully test this lab.
If you try to use your work or school email accounts, this lab has a better chance of working if you run it and try to send the email from your work computer or on campus. This is because some mail servers are set up to block sending emails from outside their domain.

TABLE OF CONTENTS
Scenario/Summary
This week, we add the ability to send e-mails from our application. Then you will develop and implement a test plan for your application. Here is an outline of the lab:
STEP A: Set up a Web Application — this should be a copy of your Week 5 Lab as a starting point.
STEP B: Implement E-mail Functionally in the Business Layer
STEP C: Add E-mail Fields to pgConfirm.aspx
STEP D: Add Send E-mail Functionality to pgConfirm.aspx.cs
STEP E: Develop a Test Plan and Test your Application
STEP F: (Optional) Add Functionality to Autofill the Recipient’s E-mail Address
STEP G: Finalize the Lab

STEP A: Set up a Web Application
To begin this week’s lab, create an empty Web Site and copy the files from the Week 5 Lab into your website folder.
Test your application. It should function the same as it did at the end of the Week 5 Lab.

STEP B: Implement E-mail Send Functionality in the Business Layer
1. Because e-mail is a form of communication, we implement the SendEmail method in the business layer. First we create a MailMessage object that has the information for the e-mail, such as To, From, Subject, and the message Body. Then, to connect to a mail server and send the e-mail, we create an SmtpClient object. We need to tell the server about these two ASP.NET classes, so place the following at the start of clsBusinessLayer.cs:
Modules for clsBusinessLayer.cs
using System.Net;
using System.Net.Mail;

2. Let’s begin by stubbing out the SendMail method. The method accepts as parameters information needed to build and send the e-mail. We also return a message to the client that called the method indicating success or failure. We’ll be optimistic and assume success. Stubbing a method is a useful development technique that lets your program compile and use the method, yet you don’t have to have all the features implemented-only the correct return type.
SendMail method stub in clsBusinessLayer.cs
public string SendEmail(string emailFrom, string emailTo, string emailSubject, string emailBody, string acctPassword)
{
string mailSendMessage = “Email Status: The message was sent successfully to: ” + emailTo;
//**** TODO: Create email message! *** //
//**** TODO: Add send mail functionality! *** //
return mailSendMessage;
}

3. Next we create the MailMessage object and set its data fields using the values in the parameters. Replace the TODO comment for creating an e-mail message with the following code:
Creating a Message in SendMail method
// Add your comments here
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(emailFrom);
objMailMessage.To.Add(new MailAddress(emailTo));
objMailMessage.Subject = emailSubject;
objMailMessage.Body = emailBody;

4. Now we create an SmptClient object to connect to your service provider and send thee-mail message.

Network Credentials
The two parameters for the NetworkCredential constructor should be the username (e-mail address) and account password for your web e-mail service. The values for these will be entered in the web form we build in a later step.
Do not hard code these values in your application—I don’t want to know your e-mail password!

Connecting to the Mail Service
Two very important fields in the SmptClient object are the Host and Port fields. These must be set to the values required by your personal e-mail service! Normally these would be set to an organization’s SMTP server, but because there is no company for this lab, you must use your own web mail service. Host values for several common web mail providers is provided in the code comments. If your provider is not listed, you can check its support instructions to see how a local client e-mail application, such as Outlook or Mac OS X Mail can connect. These two fields, alongwith the NetworkCredentials object, make it possible to send the e-mail.

Replace the TODO comment for adding send mail functionality with the following code:
Adding Send Functionality to SendEmail method
// Add your comments here
NetworkCredential objSMTPCredentials = new NetworkCredential(emailFrom,acctPassword);
// Add your comments here
SmtpClient mailObj = new SmtpClient();
// This needs to change based on your email Host
// to permit your application to send email
// for GMAIL
mailObj.Host = “smtp.gmail.com”;
// for YAHOO, YMAIL, ROCKETMAIL, YAHOOMAIL
// mailObj.Host = “smtp.mail.yahoo.com”;
// for HOTMAIL, MSN, OUTLOOK and LIVE.COM
// mailObj.Host = “smtp.live.com”;
// for AOL
// mailObj.Host = “smtp.aol.com”;
// Add your comments here
mailObj.Port = 587;
// if this does not work, try port 25
// Add your comments here
mailObj.UseDefaultCredentials = false;
mailObj.Credentials = objSMTPCredentials;
mailObj.EnableSsl = true;
mailObj.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
// Add your comments here
mailObj.Send(objMailMessage);
}
catch(Exception error)
{
mailSendMessage = ” Email Status: An error has happened sending the email, ” + emailTo;
}

STEP C: Add E-mail Fields to pgConfirm.aspx
In this step, we modify pgConfirm.aspx so the user can send an e-mail. For this application to successfully send an e-mail, it will need your e-mail credentials. You may hard code your e-mail address, but please do not hard code your password! It should be entered in the SENDAuthorization field. This field should have the TextMode property set to Password so that the password is not displayed when typed.

1. Begin by moving the Credit Card information and the Submit Order button into the left ContentArea of the Master Page. This leaves room for only our e-mail feature to be in the right ContentArea.
2. In this right ContentArea, add the controls as shown below. Structure the layout of your form to appear similar to an e-mail message.
ControlType Name or ID Text (if applicable)
Label lblFrom From:
TextBox txtFrom yourEmail@yourProvider.com
Label lblTo To:
TextBox txtTo
Label lblSubject Subject:
TextBox txtSubject
Label lblMessage Message:
TextBox txtMessage
Label lblPassword Enter SMTP account password for SEND authorization:
TextBox txtPassword
Button btnSendEmail Send Email

3. Several of the controls require the following special property settings.
Textbox txtMessage – Set the TextMode property to MultiLine so that the user can type several lines of text for the e-mail message body. You can also adjust the size of this text box to show several lines of text.
Textbox txtFrom – Set the Text property to the e-mail address from the account you use to send e-mail.
Textbox txtPassword – Set the TextMode property to Password so that your e-mail account password is not displayed.

4. When this step is complete, in Design Mode, pgConfirm.aspx should look similar to the image below:

STEP D: Sending an E-mail From pgConfirm.aspx
In pgConfirm.aspx.cs, create the method btnSendEmail_click() so that it calls SendEmail() and passes the data the user entered in the form.
1. Add a data field to represent the business layer object:
clsBusinessLayer myBusinessLayer;
2. Add the following line to the Page_Load method to instantiate the business layerobject:
myBusinessLayer = new clsBusinessLayer(Server.MapPath(“~/App_Data/”));
3. Create the btnSendMail_Click method by entering the code below into pgConfirm.aspx.cs and enter your comments for each step being performed.
btnSendMail_Click method in pgConfirm.aspx.cs
protected void btnSendEmail_Click (object sender, EventArgs e)
{
Master.UserFeedBack.Text = myBusinessLayer.SendEmail(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text, txtPassword.Text);
}

STEP E: Develop a Test Plan and Test Your Application!
1. Develop a test plan that has at least ten different features to test. You should test *the entire* application, not just the email functionality from this week. You can test more; being thorough is a good thing!
An Excel spreadsheet (Wk6_Test_Plan.xlsx (https://devryu.instructure.com/courses/60316/files/9167014/download?wrap=1)) that you can use as a test plan template can be found in the Files section of the Course Menu.

2. Test your application! Make changes as appropriate until it works. Try sending several e-mails to yourself. If your application fails to send mail, try the following adjustments.
There are usually three possible values that cause issues: email username (usually your e-mail address), your password, and the host’s STMP address.
Double-check that mailObj.Host matches the name of your e-mail or ISP’s SMTPserver.
The From e-mail address and password should be what you use to log in to your e-mail account. If your username is different, change the emailFrom parameter in the call to the NetworkCredentials constructor to reflect your e-mail account username.
A fourth, less common problem is that some e-mail providers may use ports other than 587 for SSL e-mail. Try setting mailObj.Port to either 25 or 465.

3. Execute the tests in your test plan and record the results in the spreadsheet . Upload the spreadsheet as part of this week’s deliverables.

STEP F: (OPTIONAL) Auto-fill Recipient’s E-mail Address (OPTIONAL)
OPTIONAL ACTIVITY:
Each user record in the database has an e-mail address associated with it. To have pgConfirm automatically fill the To field with this address, two small changes need to be made to the application:
1. Modify the FindCustomer method in clsBusinessLayer.cs so that it validates the e-mailaddress in addition to the other fields in the record.
2. In the Page_Load method of pgConfirm.aspx.cs, call the business layer FindCustomer method and use the result to fill the txtTo control.
Then test away!

STEP G: Finalize the Lab
1. Save your work!
2. Test it! Make changes as appropriate until it works.
3. Remember to add comments for each step being performed.
4. Please zip and submit the entire web project folder and your test plan.

992 in stock

Description

WEB460 Lab 6 of 7: Sending E-mail and Testing Your Application

Lab Overview
This lab will not work if your Gmail, Yahoo, Outlook, or other email account is set up with two-factor authentication. You should use or create an account that does not have two-factor authentication to fully test this lab.
If you try to use your work or school email accounts, this lab has a better chance of working if you run it and try to send the email from your work computer or on campus. This is because some mail servers are set up to block sending emails from outside their domain.

TABLE OF CONTENTS
Scenario/Summary
This week, we add the ability to send e-mails from our application. Then you will develop and implement a test plan for your application. Here is an outline of the lab:
STEP A: Set up a Web Application — this should be a copy of your Week 5 Lab as a starting point.
STEP B: Implement E-mail Functionally in the Business Layer
STEP C: Add E-mail Fields to pgConfirm.aspx
STEP D: Add Send E-mail Functionality to pgConfirm.aspx.cs
STEP E: Develop a Test Plan and Test your Application
STEP F: (Optional) Add Functionality to Autofill the Recipient’s E-mail Address
STEP G: Finalize the Lab

STEP A: Set up a Web Application
To begin this week’s lab, create an empty Web Site and copy the files from the Week 5 Lab into your website folder.
Test your application. It should function the same as it did at the end of the Week 5 Lab.

STEP B: Implement E-mail Send Functionality in the Business Layer
1. Because e-mail is a form of communication, we implement the SendEmail method in the business layer. First we create a MailMessage object that has the information for the e-mail, such as To, From, Subject, and the message Body. Then, to connect to a mail server and send the e-mail, we create an SmtpClient object. We need to tell the server about these two ASP.NET classes, so place the following at the start of clsBusinessLayer.cs:
Modules for clsBusinessLayer.cs
using System.Net;
using System.Net.Mail;

2. Let’s begin by stubbing out the SendMail method. The method accepts as parameters information needed to build and send the e-mail. We also return a message to the client that called the method indicating success or failure. We’ll be optimistic and assume success. Stubbing a method is a useful development technique that lets your program compile and use the method, yet you don’t have to have all the features implemented-only the correct return type.
SendMail method stub in clsBusinessLayer.cs
public string SendEmail(string emailFrom, string emailTo, string emailSubject, string emailBody, string acctPassword)
{
string mailSendMessage = “Email Status: The message was sent successfully to: ” + emailTo;
//**** TODO: Create email message! *** //
//**** TODO: Add send mail functionality! *** //
return mailSendMessage;
}

3. Next we create the MailMessage object and set its data fields using the values in the parameters. Replace the TODO comment for creating an e-mail message with the following code:
Creating a Message in SendMail method
// Add your comments here
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(emailFrom);
objMailMessage.To.Add(new MailAddress(emailTo));
objMailMessage.Subject = emailSubject;
objMailMessage.Body = emailBody;

4. Now we create an SmptClient object to connect to your service provider and send thee-mail message.

Network Credentials
The two parameters for the NetworkCredential constructor should be the username (e-mail address) and account password for your web e-mail service. The values for these will be entered in the web form we build in a later step.
Do not hard code these values in your application—I don’t want to know your e-mail password!

Connecting to the Mail Service
Two very important fields in the SmptClient object are the Host and Port fields. These must be set to the values required by your personal e-mail service! Normally these would be set to an organization’s SMTP server, but because there is no company for this lab, you must use your own web mail service. Host values for several common web mail providers is provided in the code comments. If your provider is not listed, you can check its support instructions to see how a local client e-mail application, such as Outlook or Mac OS X Mail can connect. These two fields, alongwith the NetworkCredentials object, make it possible to send the e-mail.

Replace the TODO comment for adding send mail functionality with the following code:
Adding Send Functionality to SendEmail method
// Add your comments here
NetworkCredential objSMTPCredentials = new NetworkCredential(emailFrom,acctPassword);
// Add your comments here
SmtpClient mailObj = new SmtpClient();
// This needs to change based on your email Host
// to permit your application to send email
// for GMAIL
mailObj.Host = “smtp.gmail.com”;
// for YAHOO, YMAIL, ROCKETMAIL, YAHOOMAIL
// mailObj.Host = “smtp.mail.yahoo.com”;
// for HOTMAIL, MSN, OUTLOOK and LIVE.COM
// mailObj.Host = “smtp.live.com”;
// for AOL
// mailObj.Host = “smtp.aol.com”;
// Add your comments here
mailObj.Port = 587;
// if this does not work, try port 25
// Add your comments here
mailObj.UseDefaultCredentials = false;
mailObj.Credentials = objSMTPCredentials;
mailObj.EnableSsl = true;
mailObj.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
// Add your comments here
mailObj.Send(objMailMessage);
}
catch(Exception error)
{
mailSendMessage = ” Email Status: An error has happened sending the email, ” + emailTo;
}

STEP C: Add E-mail Fields to pgConfirm.aspx
In this step, we modify pgConfirm.aspx so the user can send an e-mail. For this application to successfully send an e-mail, it will need your e-mail credentials. You may hard code your e-mail address, but please do not hard code your password! It should be entered in the SENDAuthorization field. This field should have the TextMode property set to Password so that the password is not displayed when typed.

1. Begin by moving the Credit Card information and the Submit Order button into the left ContentArea of the Master Page. This leaves room for only our e-mail feature to be in the right ContentArea.
2. In this right ContentArea, add the controls as shown below. Structure the layout of your form to appear similar to an e-mail message.
ControlType Name or ID Text (if applicable)
Label lblFrom From:
TextBox txtFrom yourEmail@yourProvider.com
Label lblTo To:
TextBox txtTo
Label lblSubject Subject:
TextBox txtSubject
Label lblMessage Message:
TextBox txtMessage
Label lblPassword Enter SMTP account password for SEND authorization:
TextBox txtPassword
Button btnSendEmail Send Email

3. Several of the controls require the following special property settings.
Textbox txtMessage – Set the TextMode property to MultiLine so that the user can type several lines of text for the e-mail message body. You can also adjust the size of this text box to show several lines of text.
Textbox txtFrom – Set the Text property to the e-mail address from the account you use to send e-mail.
Textbox txtPassword – Set the TextMode property to Password so that your e-mail account password is not displayed.

4. When this step is complete, in Design Mode, pgConfirm.aspx should look similar to the image below:

STEP D: Sending an E-mail From pgConfirm.aspx
In pgConfirm.aspx.cs, create the method btnSendEmail_click() so that it calls SendEmail() and passes the data the user entered in the form.
1. Add a data field to represent the business layer object:
clsBusinessLayer myBusinessLayer;
2. Add the following line to the Page_Load method to instantiate the business layerobject:
myBusinessLayer = new clsBusinessLayer(Server.MapPath(“~/App_Data/”));
3. Create the btnSendMail_Click method by entering the code below into pgConfirm.aspx.cs and enter your comments for each step being performed.
btnSendMail_Click method in pgConfirm.aspx.cs
protected void btnSendEmail_Click (object sender, EventArgs e)
{
Master.UserFeedBack.Text = myBusinessLayer.SendEmail(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text, txtPassword.Text);
}

STEP E: Develop a Test Plan and Test Your Application!
1. Develop a test plan that has at least ten different features to test. You should test *the entire* application, not just the email functionality from this week. You can test more; being thorough is a good thing!
An Excel spreadsheet (Wk6_Test_Plan.xlsx (https://devryu.instructure.com/courses/60316/files/9167014/download?wrap=1)) that you can use as a test plan template can be found in the Files section of the Course Menu.

2. Test your application! Make changes as appropriate until it works. Try sending several e-mails to yourself. If your application fails to send mail, try the following adjustments.
There are usually three possible values that cause issues: email username (usually your e-mail address), your password, and the host’s STMP address.
Double-check that mailObj.Host matches the name of your e-mail or ISP’s SMTPserver.
The From e-mail address and password should be what you use to log in to your e-mail account. If your username is different, change the emailFrom parameter in the call to the NetworkCredentials constructor to reflect your e-mail account username.
A fourth, less common problem is that some e-mail providers may use ports other than 587 for SSL e-mail. Try setting mailObj.Port to either 25 or 465.

3. Execute the tests in your test plan and record the results in the spreadsheet . Upload the spreadsheet as part of this week’s deliverables.

STEP F: (OPTIONAL) Auto-fill Recipient’s E-mail Address (OPTIONAL)
OPTIONAL ACTIVITY:
Each user record in the database has an e-mail address associated with it. To have pgConfirm automatically fill the To field with this address, two small changes need to be made to the application:
1. Modify the FindCustomer method in clsBusinessLayer.cs so that it validates the e-mailaddress in addition to the other fields in the record.
2. In the Page_Load method of pgConfirm.aspx.cs, call the business layer FindCustomer method and use the result to fill the txtTo control.
Then test away!

STEP G: Finalize the Lab
1. Save your work!
2. Test it! Make changes as appropriate until it works.
3. Remember to add comments for each step being performed.
4. Please zip and submit the entire web project folder and your test plan.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.