Send Email From ASPNET 2 C# Form
protected void EmailForm(object sender, EventArgs e)
{
// Build the message from the form.
string strMsg;
strMsg = GetFormData();
// Create the email message.
// Hard generic user and password here or in a web config file.
string strEmailUser = "";
string strEmailPw = "";
string strEmailAddressFrom = "";
string strEmailServer = "";
string strEmailAddressTo = "";
MailMessage emlMsg = new MailMessage();
SmtpClient emlClient = new SmtpClient();
NetworkCredential credential = new NetworkCredential(strEmailUser,strEmailPw);
emlClient.Credentials = credential;
emlClient.Host = strEmailServer;
emlMsg.To.Add(new MailAddress(strEmailAddressTo));
if (txtPosDir.Text.Length > 0) { emlMsg.To.Add(new MailAddress(txtPosDir.Text)); };
if (txtPosCeo.Text.Length > 0) { emlMsg.To.Add(new MailAddress(txtPosCeo.Text)); };
emlMsg.Subject = "Position Requisition Form";
emlMsg.From = new MailAddress(strEmailAddressFrom);
emlMsg.Body = strMsg;
// Send the message.
emlClient.Send(emlMsg);
}
private string GetFormData()
{
string strMsg = "";
strMsg = PrintData(strMsg, lblPosName, txtPosName);
strMsg = PrintData(strMsg, lblPosEffDt, txtPosEffDt);
// Line Break
strMsg = AddNewLine(strMsg);
strMsg = PrintData(strMsg, lblPosDeptName, ddlPosDeptName);
// Line Break
strMsg = AddNewLine(strMsg);
// Should have stayed with the grouped check box control;
// could have just looped on the check box collection.
strMsg = PrintChecked(strMsg, chkPosFt);
strMsg = PrintChecked(strMsg, chkPosPt);
strMsg = PrintChecked(strMsg, chkPosPrn);
strMsg = PrintChecked(strMsg, chkPosHop);
strMsg = PrintChecked(strMsg, chkPosTemp);
strMsg = PrintChecked(strMsg, chkPosDays);
strMsg = PrintChecked(strMsg, chkPosEve);
strMsg = PrintChecked(strMsg, chkPosNights);
strMsg = PrintChecked(strMsg, chkPosWeek);
strMsg = PrintChecked(strMsg, chkPosShifts);
strMsg = PrintData(strMsg, lblPosFtHours, txtPosFtHours);
strMsg = PrintData(strMsg, lblPosPtHours, txtPosPtHours);
strMsg = PrintData(strMsg, lblPosPrnHours, txtPosPrnHours);
strMsg = PrintData(strMsg, lblPosHopHours, txtPosHopHours);
// Line Break
strMsg = AddNewLine(strMsg);
// Replacement Information
strMsg = PrintChecked(strMsg, chkPosNew);
strMsg = PrintChecked(strMsg, chkPosReplace);
strMsg = PrintData(strMsg, lblPosEmployeeReplace, txtPosEmpReplace);
// Print comments / suggestions.
strMsg = PrintData(strMsg, lblPosAdvSug, txtPosAdvSug);
strMsg = PrintData(strMsg, lblPosComments, txtPosComments);
// Sign Off Information; May have to group names and date later.
strMsg = AddNewLine(strMsg);
strMsg = PrintData(strMsg, lblPosDir, txtPosDir);
strMsg = PrintData(strMsg, lblDirDate,txtPosDirdt);
// Ceo Line Break
strMsg = AddNewLine(strMsg);
strMsg = PrintData(strMsg, lblPosCeo, txtPosCeo);
strMsg = PrintData(strMsg, lblPosCeoDt, txtPosCeoDt);
return strMsg;
}
private static string AddNewLine(string strAppendNewLine)
{
strAppendNewLine += System.Environment.NewLine;
return strAppendNewLine;
}
private string PrintData(string strMsg, Label lblLabel,TextBox objText)
{
// Print new line, label and text when there is data in the text box.
if (objText.Text.Length > 0)
{
strMsg += lblLabel.Text + ' ' + objText.Text;
strMsg = AddNewLine(strMsg);
}
return strMsg;
}
private string PrintData(string strMsg, Label lblLabel, DropDownList objText)
{
// Print new line, label and text when there is data in the drop down list box.
if (objText.Text.Length > 0)
{
strMsg += lblLabel.Text + ' ' + objText.Text;
strMsg = AddNewLine(strMsg);
}
return strMsg;
}
private string PrintChecked(string strMsg,CheckBox chkBox)
{
if (chkBox.Checked)
{
strMsg += chkBox.Text;
strMsg = AddNewLine(strMsg);
}
return strMsg;
}

0 Comments:
Post a Comment
<< Home