public async System.Threading.Tasks.Task <ActionResult> SendMailAsync(EmailModel emailModel)
        {
            string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority +
                             Request.ApplicationPath.TrimEnd('/') + "/";

            EmailAPIModel emailAPIModel = new EmailAPIModel();

            emailAPIModel.subject = emailModel.subject;
            emailAPIModel.content = emailModel.content;

            emailAPIModel.sender = new Sender();
            emailAPIModel.sender.emailAddress = emailModel.sender;

            emailAPIModel.recipients = new List <Recipient>();
            if (!string.IsNullOrEmpty(emailModel.RecipientsTO))
            {
                foreach (var item in emailModel.RecipientsTO.Split(','))
                {
                    emailAPIModel.recipients.Add(
                        new Recipient
                    {
                        emailAddress  = item.Trim(),
                        recipientType = "TO",
                    });
                }
            }
            HttpResponseMessage result = new HttpResponseMessage();

            using (var content = new MultipartFormDataContent())
            {
                HttpClient httpClient = new HttpClient();

                var json          = JsonConvert.SerializeObject(emailAPIModel);
                var stringContent = new StringContent(json);

                //NOTE: convert this JSON object to the required model at the backend side
                content.Add(new StringContent(json), "mail");
                //NOTE: convert this JSON object to the required model at the backend side

                foreach (var item in emailModel.Files)
                {
                    if (item != null)
                    {
                        //content.Headers.Remove("Content-Type");
                        //content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data;");
                        content.Add(CreateFileContent(item.InputStream, item.FileName, item.ContentType), "mail");
                    }
                }

                //NOTE: change this with you API end point
                //var response = await httpClient.PostAsync("/mails/send?userID=" + emailModel.UserId, content);
                //NOTE: change this with you API end point

                var response = await httpClient.PostAsync(baseUrl + "/api/values", content);

                result = response.EnsureSuccessStatusCode();
            }
            TempData["Response"] = result.Content.ReadAsStringAsync().Result;
            return(RedirectToAction("SendMailAsync"));
        }
Esempio n. 2
0
        public string Post()
        {
            EmailAPIModel emailAPIModel  = new EmailAPIModel();
            ApprovalModel approvalModel  = new ApprovalModel();
            var           httpPostedFile = HttpContext.Current.Request.Files;
            var           data           = httpPostedFile.Count;
            var           fdata          = HttpContext.Current.Request.Form;
            var           model          = fdata["mail"];

            if (model == null)
            {
                model = fdata["approval"];
            }

            //emailAPIModel = JsonConvert.DeserializeObject<EmailAPIModel>(model);
            approvalModel = JsonConvert.DeserializeObject <ApprovalModel>(model);
            string             str     = "";
            int                count   = 0;
            HttpFileCollection uploads = HttpContext.Current.Request.Files;

            if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/images")))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/images"));
            }
            for (int i = 0; i < uploads.Count; i++)
            {
                HttpPostedFile upload       = uploads[i];
                string         targetFolder = HttpContext.Current.Server.MapPath("~/images");
                string         targetPath   = Path.Combine(targetFolder, upload.FileName);
                upload.SaveAs(targetPath);
            }
            string files = count + " Uploaded Files From:" + str;

            return("Success");
        }
Esempio n. 3
0
        public EmailAPIResultModel Post([FromBody] EmailAPIModel emailAPIModel)
        {
            int    mediaId     = -1;
            string errorString = "";

            try
            {
                if (emailAPIModel == null)
                {
                    errorString = T("The provided data does not correspond to the required format.").ToString();
                }
                else
                {
                    ContactFormRecord contactForm = _contactFormService.GetContactForm(emailAPIModel.ContentId);

                    if (contactForm == null)
                    {
                        errorString = T("The content Id has not been provided or does not correspond to a content part of the correct type.").ToString();
                    }

                    if (errorString == "")
                    {
                        errorString = _contactFormService.ValidateAPIRequest(emailAPIModel.SenderName, emailAPIModel.SendFrom, emailAPIModel.MessageText, contactForm.RequireNameField, (emailAPIModel.Attachment.Length > 0), contactForm.RequireAttachment);
                    }

                    if (errorString == "" && !string.IsNullOrWhiteSpace(emailAPIModel.Attachment) && !string.IsNullOrWhiteSpace(emailAPIModel.AttachmentName))
                    {
                        if (_contactFormService.FileAllowed(emailAPIModel.AttachmentName))
                        {
                            mediaId = _contactFormService.UploadFromBase64(emailAPIModel.Attachment, contactForm.PathUpload, emailAPIModel.AttachmentName);
                        }
                        else
                        {
                            errorString = T("The file extension in the filename is not allowed or has not been provided.").ToString();
                        }
                    }

                    if (errorString == "")
                    {
                        _contactFormService.SendContactEmail(emailAPIModel.SenderName, emailAPIModel.SendFrom, emailAPIModel.SendFrom, emailAPIModel.MessageSubject, emailAPIModel.MessageText, mediaId, contactForm, emailAPIModel.AdditionalData);
                    }
                }
            }
            catch (Exception e)
            {
                errorString = e.Message;
            }

            return(new EmailAPIResultModel {
                Error = errorString, Information = ""
            });
        }