protected void submit_Click(object sender, EventArgs e) { supportRequest request = new supportRequest(); request.Name = Cname.Value; request.Email = Cemail.Value; request.phoneNumber = Cnumber.Value; request.Product = ProductDrop.SelectedItem.Text; request.Topic = Topic.Value; request.Version = Version.SelectedItem.Text; request.Server = server.SelectedItem.Text; request.Database = Database.SelectedItem.Text; request.Enviroment = Enviroment.SelectedItem.Text; request.Subject = subject.Value; request.Stoppage = stoppage.Checked; request.Details = IssueDetails.InnerText.Replace(System.Environment.NewLine, "<br />"); request.Company = companyName.Value; request.Department = Department.Value; request.userType = userType.SelectedItem.Text; if (upload1.PostedFile != null && upload1.PostedFile.ContentLength > 0) { request.file1 = upload1.PostedFile; } if (upload2.PostedFile != null && upload2.PostedFile.ContentLength > 0) { request.file2 = upload2.PostedFile; } Email email = new Email(); bool hasSent = email.sendEmail(request); if (hasSent) { Response.Redirect("~/Completed.aspx"); } else { Response.Redirect("~/Error.aspx"); } }
protected string GetTemplate( supportRequest request) { using(StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath("~/EmailTemplate.html"))) { string body = reader.ReadToEnd(); body = body.Replace("{Name}", request.Name); body = body.Replace("{Email}", request.Email); body = body.Replace("{Number}", request.phoneNumber); body = body.Replace("{Product}", request.Product); body = body.Replace("{Topic}", request.Topic); body = body.Replace("{Version}", request.Version); body = body.Replace("{Server}", request.Server); body = body.Replace("{Database}", request.Database); body = body.Replace("{Enviroment}", request.Enviroment); body = body.Replace("{Subject}", request.Subject); body = body.Replace("{Stoppage}", request.Stoppage.ToString()); body = body.Replace("{Details}", request.Details); body = body.Replace("{Company}", request.Company); body = body.Replace("{Department}", request.Department); body = body.Replace("{UserType}", request.userType); return body; } }
public bool sendEmail(supportRequest request) { MailMessage msg = new MailMessage(); //add recipient msg.To.Add(new MailAddress("*****@*****.**", "Tom Cooper")); //add subject msg.Subject = "New Support Request: " + request.Subject; //Get HTML Template for Body msg.Body = GetTemplate(request); //set html flag msg.IsBodyHtml = true; //create from address msg.From = new MailAddress( ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpUserDisplay"] ); //Add attachment if (request.file1 != null && request.file1.FileName != null) { msg.Attachments.Add(new Attachment( request.file1.InputStream, Path.GetFileName(request.file1.FileName) )); } if (request.file2 != null && request.file2.FileName != null) { msg.Attachments.Add(new Attachment( request.file2.InputStream, Path.GetFileName(request.file2.FileName) )); } //create smtp client using (SmtpClient client = new SmtpClient( ConfigurationManager.AppSettings["smtpServer"], int.Parse(ConfigurationManager.AppSettings["smtpPort"]) )) { client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential( ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPassword"] ); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = true; try { client.Send(msg); } catch (Exception ex) { client.Dispose(); return false; } finally { client.Dispose(); } } return true; }