Пример #1
0
        /// <summary>
        /// Sends an invite email to the given email with the given message.
        /// </summary>
        /// <param name="inviteEmail">The email of the person to invite.</param>
        /// <param name="inviteMessage">The message to send with the invitation.</param>
        /// <returns>Success of the call.</returns>
        public ActionResult AJAX_InviteUser(string inviteEmail, string inviteMessage, long teamID)
        {
            string successMessage = "Message sent to " + inviteEmail;

            // Make sure the request is authenticated
            if (Request.IsAuthenticated) {

                // Make sure the invite email is bound
                if (inviteEmail != null && !inviteEmail.Equals("")) {

                    // Validate the request
                    DBAccessor dba = new DBAccessor();
                    Person user = dba.GetPersonInformation(User.Identity.Name);
                    string name = user.firstName + " " + user.lastName;
                    Team team = dba.GetTeamDetails(teamID);

                    if (team.coaches.Contains(user, new PersonComparer())) {

                        try {
                            // Add the invite to the database
                            long inviteID = dba.AddInvite(inviteEmail, user.ID, teamID);

                            // Form an email
                            String body = "";
                            if (inviteMessage != null && !inviteMessage.Equals("")) {
                                body += "See " + name + "'s message below:\n\n" + inviteMessage + "\n\n";
                            }
                            body += "To join the " + team.name + " visit http://dugoutdigits.com/Team/Join?id=" + inviteID + "&email=" + inviteEmail + " and follow the instructions.";
                            MailMessage newMessage = new MailMessage();
                            SmtpClient mailService = new SmtpClient();

                            //set the addresses
                            newMessage.From = new MailAddress(AppConstants.EMAIL_ADMIN);
                            newMessage.To.Add(inviteEmail);

                            //set the content
                            newMessage.Subject = name + " has invited you to join the " + team.name;
                            newMessage.Body = body;

                            //send the message
                            mailService.UseDefaultCredentials = false;
                            mailService.DeliveryMethod = SmtpDeliveryMethod.Network;
                            mailService.Host = AppConstants.EMAIL_SMTP_ADDRESS;
                            mailService.Credentials = new NetworkCredential(AppConstants.EMAIL_SMTP_USERNAME, AppConstants.EMAIL_SMTP_PASSWORD);
                            mailService.Send(newMessage);
                        }
                        catch (Exception) {
                            successMessage = "Error sending email to " + inviteEmail;
                        }
                    }
                    else {
                        successMessage = "Invalid attempt to invite user.";

                        LogEntry entry = new LogEntry(LogType.INVALID_REQUEST, LogFunction.INVITE_USER, LogAction.NA);
                        entry.User = user;
                        entry.Message = "Attempt to invite "+inviteEmail+" to join "+team.name+" (ID "+team.ID+").";
                        dba.LogMessage(entry);
                    }
                }
                else {
                    successMessage = "Please enter the email of the person you are trying to invite.";
                }
            }
            else {
                successMessage = "The request was not authenticated.";
            }

            // Return the success message of the addition
            return Json(
                new { message = successMessage },
                JsonRequestBehavior.AllowGet
            );
        }