· 10 years ago · Jun 25, 2016, 07:48 AM
1[HttpPost]
2 [ValidateAntiForgeryToken]
3 public async Task<ActionResult> Register(TalentInfo model, IEnumerable<HttpPostedFileBase> files)
4 {
5 if (ModelState.IsValid)
6 {
7 RegisterRepository regRepo = new RegisterRepository();
8
9 if (regRepo.Register(model))
10 {
11 List<string> paths = new List<string>();
12
13 foreach (var file in files)
14 {
15 if (file.ContentLength > 0)
16 {
17 var fileName = Path.GetFileName(file.FileName);
18 var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
19 file.SaveAs(path);
20 paths.Add(path);
21 }
22 }
23
24 var message = new MailMessage();
25 foreach (var path in paths)
26 {
27 var fileInfo = new FileInfo(path);
28 var memoryStream = new MemoryStream();
29 using (var stream = fileInfo.OpenRead())
30 {
31 stream.CopyTo(memoryStream);
32 }
33 memoryStream.Position = 0;
34 string fileName = fileInfo.Name;
35 message.Attachments.Add(new Attachment(memoryStream, fileName));
36 }
37
38 //Rest of business logic here
39 string EncodedResponse = Request.Form["g-Recaptcha-Response"];
40 bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
41 if (IsCaptchaValid)
42 {
43
44 var body = "<p><b>Email From:</b> {0} ({1})</p><p><b>Message:<b></p><p>{2}</p>";
45 message.To.Add(new MailAddress("***@gmail.com")); // replace with valid value
46 message.From = new MailAddress("***@ymailcom"); // replace with valid value
47 message.Subject = "YesDubai.org (REGISTRATION)";
48 message.Body = string.Format(body, model.Talent_Name, model.Talent_Email, model.Talent_SelfPromotion);
49 message.IsBodyHtml = true;
50 using (var smtp = new SmtpClient())
51 {
52 var credential = new NetworkCredential
53 {
54 UserName = "***@gmail.com", // replace with valid value
55 Password = "***" // replace with valid value
56 };
57 smtp.Credentials = credential;
58 smtp.Host = "smtp.gmail.com";
59 smtp.Port = 587;
60 smtp.EnableSsl = true;
61 smtp.SendCompleted += (s, e) =>
62 {
63 //delete attached files
64 foreach (var path in paths)
65 System.IO.File.Delete(path);
66 };
67 await smtp.SendMailAsync(message);
68 ViewBag.Message = "Your message has been sent!";
69
70 ModelState.Clear();
71 return View("Register");
72 }
73 }
74 else
75 {
76 TempData["recaptcha"] = "Please verify that you are not a robot!";
77 }
78
79 } return View(model);
80
81 }
82 }
83
84public partial class TalentInfo
85{
86 [Display(Name = "ID")]
87 public int TalentID { get; set; }
88 [Display(Name = "Talent's Name")]
89 public string Talent_Name { get; set; }
90 [Display(Name = "Email Address")]
91 public string Talent_Email { get; set; }
92 [Display(Name = "Self Promotion")]
93 public string Talent_SelfPromotion { get; set; }
94}
95
96public class RegisterRepository
97{
98
99 //SqlTransaction transaction = null;
100 private SqlConnection con;
101 //To Handle connection related activities
102 private void connection()
103 {
104 string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
105 con = new SqlConnection(constr);
106
107
108 }
109
110
111 internal bool Register(TalentInfo model)
112 {
113 throw new NotImplementedException();
114
115 connection();
116
117 try
118 {
119 SqlCommand com = new SqlCommand("SP_INSERT_TALENT_INFO", con);
120 com.CommandType = CommandType.StoredProcedure;
121 com.Parameters.AddWithValue("@Talent_name", model.Talent_Name);
122 com.Parameters.AddWithValue("@Talent_email", model.Talent_Email);
123 com.Parameters.AddWithValue("@Talent_SelfPromotion", model.Talent_SelfPromotion);
124 con.Open();
125 int i = com.ExecuteNonQuery();
126 con.Close();
127 if (i >= 1)
128 {
129 return true;
130 }
131 else
132 {
133
134 return false;
135 }
136
137
138 }
139
140 catch
141 {
142 return Register(model);
143 }
144 finally
145 {
146 con.Close();
147 }
148
149 }
150
151
152}