Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ExchangeServerReminderService : IMailServerReminderService
- {
- readonly Logger logger = LogManager.GetCurrentClassLogger();
- readonly ExchangeService exchangeService;
- readonly IBus bus;
- private readonly string recruitmentGroupEmail;
- public ExchangeServerReminderService(string exchangeAuthUsername,
- string exchangeAuthPassword,
- string autodiscoverUrl,
- string recruitmentGroupEmail,
- IBus bus)
- {
- if (String.IsNullOrWhiteSpace(exchangeAuthUsername))
- throw new ArgumentNullException("exchangeAuthUsername");
- if (String.IsNullOrWhiteSpace(exchangeAuthPassword))
- throw new ArgumentNullException("exchangeAuthPassword");
- if (String.IsNullOrWhiteSpace(autodiscoverUrl))
- throw new ArgumentNullException("autodiscoverUrl");
- if (String.IsNullOrWhiteSpace(recruitmentGroupEmail))
- throw new ArgumentNullException("recruitmentGroupEmail");
- if (bus == null)
- throw new ArgumentNullException("bus");
- this.bus = bus;
- this.recruitmentGroupEmail = recruitmentGroupEmail;
- exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
- exchangeService.Credentials = new WebCredentials(exchangeAuthUsername, exchangeAuthPassword);
- exchangeService.AutodiscoverUrl(autodiscoverUrl, url => true);
- }
- public ExchangeServerReminderService(string exchangeAuthUsername,
- string exchangeAuthPassword,
- string recruitmentGroupEmail,
- IBus bus)
- {
- if (String.IsNullOrWhiteSpace(exchangeAuthUsername))
- throw new ArgumentNullException("exchangeAuthUsername");
- if (String.IsNullOrWhiteSpace(exchangeAuthPassword))
- throw new ArgumentNullException("exchangeAuthPassword");
- if (String.IsNullOrWhiteSpace(recruitmentGroupEmail))
- throw new ArgumentNullException("recruitmentGroupEmail");
- if (bus == null)
- throw new ArgumentNullException("bus");
- this.bus = bus;
- this.recruitmentGroupEmail = recruitmentGroupEmail;
- exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
- exchangeService.Credentials = new WebCredentials(exchangeAuthUsername, exchangeAuthPassword);
- exchangeService.AutodiscoverUrl(exchangeAuthUsername, url => true);
- }
- public string CreateOrUpdateAppointment(string subject, string body, DateTime startTime, string exchangeServerAppointmentId, DateTime eventDate, DateTime remindAt, IEnumerable<Document> cv)
- {
- if (String.IsNullOrWhiteSpace(subject))
- throw new ArgumentNullException("subject");
- if (String.IsNullOrWhiteSpace(body))
- throw new ArgumentNullException("body");
- if (startTime == default(DateTime))
- throw new ArgumentException("startTime");
- Appointment appointment;
- try
- {
- appointment = Appointment.Bind(exchangeService, exchangeServerAppointmentId);
- appointment.Subject = subject;
- appointment.Resources.Clear();
- appointment.Body = body;
- appointment.Body.BodyType = BodyType.HTML;
- appointment.Start = startTime;
- appointment.End = startTime;
- appointment.ReminderMinutesBeforeStart = (int)(eventDate - remindAt).TotalMinutes;
- appointment.RequiredAttendees.Clear();
- appointment.RequiredAttendees.Add(recruitmentGroupEmail);
- appointment.Attachments.Clear();
- foreach (var attachment in cv)
- {
- appointment.Attachments.AddFileAttachment(attachment.Name + attachment.Extension, attachment.Content);
- }
- appointment.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendOnlyToAll);
- return appointment.Id.UniqueId;
- }
- catch (Exception e)
- {
- using (var principalContext = new PrincipalContext(ContextType.Domain, Environment.UserDomainName))
- {
- var up = UserPrincipal.FindByIdentity(principalContext, HttpContext.Current.User.Identity.Name);
- //appointment not found
- appointment = new Appointment(exchangeService)
- {
- Subject = subject,
- Body = body
- };
- appointment.Body.BodyType = BodyType.HTML;
- appointment.Start = startTime;
- appointment.End = startTime;
- appointment.RequiredAttendees.Add(recruitmentGroupEmail);
- appointment.ReminderMinutesBeforeStart = (int)(eventDate - remindAt).TotalMinutes;
- appointment.Importance = Importance.High;
- foreach (var attachment in cv)
- {
- appointment.Attachments.AddFileAttachment(attachment.Name + attachment.Extension, attachment.Content);
- }
- appointment.Save(new FolderId(WellKnownFolderName.Calendar, up.EmailAddress), SendInvitationsMode.SendOnlyToAll);
- return appointment.Id.UniqueId;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement