コード例 #1
0
        public bool SendMail(SendMailBindingModel model, string host, int port, string mailCredential, string passCredential, string recepient)
        {
            try
            {
                if (host == null || mailCredential == null || passCredential == null ||
                    recepient == null)
                {
                    return(false);
                }
                SmtpClient smtpClient = new SmtpClient(host, port);

                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials           = new System.Net.NetworkCredential(mailCredential, passCredential);
                smtpClient.DeliveryMethod        = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl             = true;

                MailMessage theMessage = new MailMessage();

                theMessage.From = new MailAddress(model.Email);
                theMessage.To.Add(recepient);
                theMessage.Subject = model.Subject;
                theMessage.Body    = model.Message;
                smtpClient.Send(theMessage);

                return(true);
            }
            catch (SystemException)
            {
                return(false);
            }
        }
コード例 #2
0
        public async Task <IHttpActionResult> PutMail(SendMailBindingModel model)
        {
            ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

            Message resultMsg = this.repository.UpdateMessage(user.Email, model.ID, model.To, model.Subject, model.Body);

            var result = new
            {
                resultMsg = resultMsg,
                result    = resultMsg != null ? "Mail updated successfully" : "Error updating mail"
            };

            return(Ok(result));
        }
コード例 #3
0
        public async Task <IHttpActionResult> Post(SendMailBindingModel model)
        {
            ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

            Account acc = this.repository.GetAccountFromAddress(user.Email);

            this.mailBox = new EmailSettings(
                user.Email,
                user.UserName,
                acc.Password
                //Helper.GetDomainNameFromAddress(this.user.Email)    //domain
                );

            if (model.IsDraft)
            {
                //Draft mail does not require model validations
                bool saveSucceed = this.repository.SaveMessageAsDraft(this.mailBox, new List <string> {
                    model.To
                }, model.Subject, model.Body);
                return(Ok(saveSucceed ? "Your mail has been saved as draft" : "Error saving mail"));
            }
            else
            {
                ToModel tm = Helper.CheckEmailInputFromClient(model.To);

                if (!tm.IsValid)
                {
                    ModelState.AddModelError("To", "Invalid recipient address(es)");
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                //System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(
                //    user.Email,
                //    model.To, // To
                //    model.Subject, // Subject
                //    model.Body); // Body

                //bool IsSuccessful = this.repository.SendMessage(this.mailBox, mailMessage);

                bool sendSucceed = this.repository.SendMessage(this.mailBox, tm.Addresses, model.Subject, model.Body);

                return(Ok(sendSucceed ? "Your mail has been sent" : "Error sending mail"));
            }
        }
コード例 #4
0
        public ActionResult SendMail(SendMailBindingModel model)
        {
            var sendMailresult = this.service.SendMail(
                model,
                Consts.GmailSmtpHost,
                Consts.GmailSmtpPort,
                Consts.AdminMailCredential,
                Consts.AdminPassowrdCredential,
                Consts.AdminMailRecepient);

            if (sendMailresult)
            {
                return(RedirectToAction("MessageSent", "About"));
            }

            return(RedirectToAction("Index", "Home"));
        }