Beispiel #1
0
        public async Task <IRestResponse> SendAppointmentMail(object appointmentId, UserProfile userProfile)
        {
            if (userProfile == null || !int.TryParse(appointmentId.ToString(), out var id))
            {
                throw new InvalidOperationException(); //..???
            }

            _userProfile = userProfile;
            _appointment = _petShelterDbContext.Appointments
                           .Include(x => x.Pet.Shelter)
                           .FirstOrDefault(x => x.Id == id);

            RestClient client = new RestClient
            {
                BaseUrl       = new Uri(_configuration["Mailgun:Audience"]),
                Authenticator = new HttpBasicAuthenticator("api", _configuration["Mailgun:Token"])
            };

            var request = new RestRequest();

            request.AddParameter("domain", _configuration["Mailgun:Domain"], ParameterType.UrlSegment);
            request.Resource = "{domain}/messages.mime";
            request.AddParameter("to", _configuration["Mailgun:TestMail"]);
            var mail = new AppointmentMail()
            {
                To          = _configuration["Mailgun:TestMail"],
                From        = _configuration["Mailgun:From"],
                Subject     = $"Je afspraak met {_appointment.Pet.Name}!",
                Text        = string.Join(Environment.NewLine, GetPlainText()),
                TextHtml    = string.Concat(GetHtmlBody(GetPlainText())),
                Start       = _appointment.Start,
                PetName     = _appointment.Pet.Name,
                ICalMessage = GetIcalMessage()
            };

            request.AddFile(
                "message",
                Encoding.UTF8.GetBytes(GetMailWithICalInvite(mail)),
                "message.mime");
            request.Method = Method.POST;

            var cancellationTokenSource = new CancellationTokenSource();

            return(await client.ExecuteTaskAsync(request, cancellationTokenSource.Token));
        }
        public ActionResult AppointmentByStudent(AppointmentByStudentModel model)
        {
            // A student wishes to make an appointment with their counseler.
            if (ModelState.IsValid)
            {
                // Get the logged in student and counseler of the class the student is in.
                var student   = Student;
                var @class    = _classRepository.GetById(student.ClassId);
                var counseler = CounselerRepository.GetById(@class.CounselerId);

                if (counseler != null && student != null)
                {
                    // Create the appointment for the student and couseler.
                    var appointment = new Appointment
                    {
                        CounselerId = counseler.Id,
                        StudentId   = student.Id,
                        DateTime    = model.DateTime,
                        Location    = model.Location
                    };

                    int id = _appointmentRepository.Add(appointment);

                    // Send the appointment request mail.
                    var mail = new AppointmentMail
                    {
                        Counseler     = counseler,
                        Student       = student,
                        DateTime      = appointment.DateTime,
                        Location      = appointment.Location,
                        AcceptUrl     = FullUrl("AppointmentResponse", new { id }),
                        FromCounseler = false
                    };

                    _mailHelper.SendAppointmentMail(mail);

                    return(RedirectToAction("Details", "Appointment", new { id }));
                }
            }

            PrepareStudentCounselingRequestModel(model);
            return(View(model));
        }
        public ActionResult AppointmentByCounseler(AppointmentByCounselerModel model)
        {
            if (ModelState.IsValid)
            {
                // Get the logged in counseler and the chosen student.
                var counseler = Counseler;
                var student   = StudentRepository.GetById(model.StudentId);
                if (counseler != null && student != null)
                {
                    // Create the appointment for the counseler and student.
                    var appointment = new Appointment
                    {
                        CounselerId = counseler.Id,
                        StudentId   = model.StudentId,
                        DateTime    = model.DateTime,
                        Location    = model.Location
                    };

                    int id = _appointmentRepository.Add(appointment);

                    // Send the appointment mail.
                    var mail = new AppointmentMail
                    {
                        Counseler     = counseler,
                        Student       = student,
                        DateTime      = appointment.DateTime,
                        Location      = appointment.Location,
                        AcceptUrl     = FullUrl("AppointmentResponse", new { id }),
                        FromCounseler = true
                    };
                    _mailHelper.SendAppointmentMail(mail);

                    return(RedirectToAction("Details", "Appointment", new { id }));
                }
            }

            PrepareCounselerCounselingRequestModel(model);
            return(View(model));
        }
Beispiel #4
0
        private string GetMailWithICalInvite(AppointmentMail message)
        {
            var textBody = new TextPart("plain")
            {
                Text = message.Text
            };
            var htmlBody = new TextPart("html")
            {
                Text = message.TextHtml
            };

            // add views to the multipart/alternative
            var alternative = new Multipart("alternative")
            {
                textBody,
                htmlBody
            };

            if (!string.IsNullOrWhiteSpace(message.ICalMessage))
            {
                // also add the calendar as an alternative view
                // encoded as base64, but 7bit will also work
                var calendarBody = new TextPart("calendar")
                {
                    Text = message.ICalMessage,
                    ContentTransferEncoding = ContentEncoding.Base64
                };

                // most clients wont recognise the alternative view without the
                // method=REQUEST header
                calendarBody.ContentType.Parameters.Add("method", "REQUEST");
                alternative.Add(calendarBody);
            }

            // create the multipart/mixed that will contain the multipart/alternative
            // and all attachments
            var multiPart = new Multipart("mixed")
            {
                alternative
            };

            if (!string.IsNullOrWhiteSpace(message.ICalMessage))
            {
                // add the calendar as an attachment
                var calAttachment = new MimePart("application", "ics")
                {
                    ContentDisposition      = new ContentDisposition(ContentDisposition.Attachment),
                    ContentTransferEncoding = ContentEncoding.Base64,
                    FileName = "invite.ics",
                    Content  = new MimeContent(GenerateStreamFromString(message.ICalMessage))
                };

                multiPart.Add(calAttachment);
            }

            // TODO: Add any other attachements to 'multipart' here.

            // build final mime message
            var mimeMessage = new MimeMessage();

            mimeMessage.From.Add(new MailboxAddress("petshelter", message.From));
            mimeMessage.To.Add(new MailboxAddress(
                                   _userProfile.Email.Substring(0, _userProfile.Email.IndexOf('@')),
                                   _configuration["Mailgun:TestMail"]));
            mimeMessage.Subject = message.Subject;
            mimeMessage.Body    = multiPart;

            // parse and return mime message
            return(mimeMessage.ToString());
        }