I am creating a job application web form, where applicants apply and when submit is clicked. The information is stored in a database and it is also sent to an email. I ran my code and everything works with no error showing. The only problem was that the email was not sent. Thanks in advance!
Here is the code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName1 = Server.MapPath("~/App_Data/Applicant.txt");
string mailBody = File.ReadAllText(fileName1);
mailBody = mailBody.Replace("##position##", lblPosition.Text);
mailBody = mailBody.Replace("##firstName##", txtFirstName.Text);
mailBody = mailBody.Replace("##lastName##", txtLastName.Text);
mailBody = mailBody.Replace("##phone##", txtPhone.Text);
mailBody = mailBody.Replace("##email##", txtEmail.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Wired Job Applicant";
myMessage.Body = mailBody;
myMessage.From = new MailAddress("intern2@wireddistributing.com", "Sender Name");
myMessage.To.Add(new MailAddress("intern2@wireddistributing.com", "Receiver Name"));
myMessage.ReplyToList.Add(new MailAddress(txtEmail.Text));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
using (WiredEntities myEntities = new WiredEntities())
{
Applicant myApplicant;
myApplicant = new Applicant();
myEntities.Applicants.Add(myApplicant);
myApplicant.FirstName = txtFirstName.Text;
myApplicant.LastName = txtLastName.Text;
myApplicant.Phone = txtPhone.Text;
myApplicant.Email = txtEmail.Text;
Job_Applicant jobApplication;
jobApplication = new Job_Applicant();
jobApplication.DateSubmitted = DateTime.Now;
myEntities.Job_Applicant.Add(jobApplication);
jobApplication.ApplicantId = myApplicant.ApplicantId;
jobApplication.JobId = _id;
if (FileUpload1.HasFile)
{
string virtualFolder = "~/Resumes/";
string phyisicalFolder = Server.MapPath(virtualFolder);
string fileName = FileUpload1.FileName;
FileUpload1.SaveAs(System.IO.Path.Combine(phyisicalFolder, fileName));
jobApplication.FileUrl = virtualFolder + fileName;
}
myEntities.SaveChanges();
Response.Redirect("Careers.aspx");
}
}
}