public async Task <IActionResult> AddUpload(UploadViewModel model) { if (ModelState.IsValid) { string unique = Guid.NewGuid().ToString(); string UniqueTransId = "Upload-" + unique; var upload = new UploadDoc { Name = model.Name, Email = model.Email, TransNumber = UniqueTransId }; _upload.AddUploadDoc(upload); string webrootPath = _hostEnvironment.WebRootPath; var files = model.FormFiles; var uploads = Path.Combine(webrootPath, "images"); for (int i = 0; i < files.Count; i++) { string fileName = files[i].FileName; //var extension = Path.GetExtension(files[i].FileName); using (var fileStreams = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { files[i].CopyTo(fileStreams); } var uploadFile = new UploadImage { UploadId = upload.Id, ImagePath = @"\images\" + fileName }; _upload.AddUploadFile(uploadFile); } #region Mail MailMessage msg = new MailMessage // instance Mail sender service { From = new MailAddress("#########################################"), // Server Email Address }; msg.To.Add(model.Email); // receiver Email msg.Subject = "SDSD Developer Test"; msg.Body = "Hello " + model.Name + ", these are your attachments"; // Message Body foreach (var filepath in files) { string fileName = Path.GetFileName(filepath.FileName); msg.Attachments.Add(new Attachment(filepath.OpenReadStream(), fileName)); } SmtpClient client = new SmtpClient { Host = "smtp.gmail.com" }; NetworkCredential credential = new NetworkCredential { // Server Email credentisal UserName = "******", Password = "******" }; client.Credentials = credential; client.EnableSsl = true; client.Port = 587; client.Send(msg); ViewBag.Success = $"Email has been sent successfully to {model.Email}"; _logger.LogInformation("User created "); #endregion if (await _upload.SaveChangesAsync()) { return(RedirectToAction(nameof(Index))); } return(View(model)); } return(View(model)); }