Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.61 KB | None | 0 0
  1. class ExchangeServerReminderService : IMailServerReminderService
  2.     {
  3.         readonly Logger logger = LogManager.GetCurrentClassLogger();
  4.         readonly ExchangeService exchangeService;
  5.         readonly IBus bus;
  6.         private readonly string recruitmentGroupEmail;
  7.  
  8.         public ExchangeServerReminderService(string exchangeAuthUsername,
  9.            string exchangeAuthPassword,
  10.            string autodiscoverUrl,
  11.            string recruitmentGroupEmail,
  12.            IBus bus)
  13.         {
  14.             if (String.IsNullOrWhiteSpace(exchangeAuthUsername))
  15.                 throw new ArgumentNullException("exchangeAuthUsername");
  16.             if (String.IsNullOrWhiteSpace(exchangeAuthPassword))
  17.                 throw new ArgumentNullException("exchangeAuthPassword");
  18.             if (String.IsNullOrWhiteSpace(autodiscoverUrl))
  19.                 throw new ArgumentNullException("autodiscoverUrl");
  20.  
  21.             if (String.IsNullOrWhiteSpace(recruitmentGroupEmail))
  22.                 throw new ArgumentNullException("recruitmentGroupEmail");
  23.             if (bus == null)
  24.                 throw new ArgumentNullException("bus");
  25.  
  26.             this.bus = bus;
  27.             this.recruitmentGroupEmail = recruitmentGroupEmail;
  28.             exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
  29.             exchangeService.Credentials = new WebCredentials(exchangeAuthUsername, exchangeAuthPassword);
  30.             exchangeService.AutodiscoverUrl(autodiscoverUrl, url => true);
  31.         }
  32.  
  33.         public ExchangeServerReminderService(string exchangeAuthUsername,
  34.             string exchangeAuthPassword,
  35.             string recruitmentGroupEmail,
  36.             IBus bus)
  37.         {
  38.             if (String.IsNullOrWhiteSpace(exchangeAuthUsername))
  39.                 throw new ArgumentNullException("exchangeAuthUsername");
  40.             if (String.IsNullOrWhiteSpace(exchangeAuthPassword))
  41.                 throw new ArgumentNullException("exchangeAuthPassword");
  42.  
  43.             if (String.IsNullOrWhiteSpace(recruitmentGroupEmail))
  44.                 throw new ArgumentNullException("recruitmentGroupEmail");
  45.             if (bus == null)
  46.                 throw new ArgumentNullException("bus");
  47.  
  48.             this.bus = bus;
  49.             this.recruitmentGroupEmail = recruitmentGroupEmail;
  50.             exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
  51.             exchangeService.Credentials = new WebCredentials(exchangeAuthUsername, exchangeAuthPassword);
  52.             exchangeService.AutodiscoverUrl(exchangeAuthUsername, url => true);
  53.         }
  54.  
  55.      
  56.         public string CreateOrUpdateAppointment(string subject, string body, DateTime startTime, string exchangeServerAppointmentId, DateTime eventDate, DateTime remindAt, IEnumerable<Document> cv)
  57.         {
  58.             if (String.IsNullOrWhiteSpace(subject))
  59.                 throw new ArgumentNullException("subject");
  60.             if (String.IsNullOrWhiteSpace(body))
  61.                 throw new ArgumentNullException("body");
  62.             if (startTime == default(DateTime))
  63.                 throw new ArgumentException("startTime");
  64.  
  65.             Appointment appointment;
  66.             try
  67.             {
  68.                 appointment = Appointment.Bind(exchangeService, exchangeServerAppointmentId);
  69.                 appointment.Subject = subject;
  70.                 appointment.Resources.Clear();
  71.                 appointment.Body = body;
  72.                 appointment.Body.BodyType = BodyType.HTML;
  73.                 appointment.Start = startTime;
  74.                 appointment.End = startTime;
  75.                 appointment.ReminderMinutesBeforeStart = (int)(eventDate - remindAt).TotalMinutes;
  76.                 appointment.RequiredAttendees.Clear();
  77.                 appointment.RequiredAttendees.Add(recruitmentGroupEmail);
  78.                 appointment.Attachments.Clear();
  79.                 foreach (var attachment in cv)
  80.                 {
  81.                     appointment.Attachments.AddFileAttachment(attachment.Name + attachment.Extension, attachment.Content);
  82.                 }
  83.                 appointment.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendOnlyToAll);
  84.                 return appointment.Id.UniqueId;
  85.  
  86.             }
  87.             catch (Exception e)
  88.             {
  89.                 using (var principalContext = new PrincipalContext(ContextType.Domain, Environment.UserDomainName))
  90.                 {
  91.                     var up = UserPrincipal.FindByIdentity(principalContext, HttpContext.Current.User.Identity.Name);
  92.                     //appointment not found
  93.                     appointment = new Appointment(exchangeService)
  94.                     {
  95.                         Subject = subject,
  96.                         Body = body
  97.                     };
  98.                     appointment.Body.BodyType = BodyType.HTML;
  99.                     appointment.Start = startTime;
  100.                     appointment.End = startTime;
  101.                     appointment.RequiredAttendees.Add(recruitmentGroupEmail);
  102.                     appointment.ReminderMinutesBeforeStart = (int)(eventDate - remindAt).TotalMinutes;
  103.                     appointment.Importance = Importance.High;
  104.                     foreach (var attachment in cv)
  105.                     {
  106.                         appointment.Attachments.AddFileAttachment(attachment.Name + attachment.Extension, attachment.Content);
  107.                     }
  108.  
  109.                     appointment.Save(new FolderId(WellKnownFolderName.Calendar, up.EmailAddress), SendInvitationsMode.SendOnlyToAll);
  110.  
  111.                     return appointment.Id.UniqueId;
  112.                 }
  113.             }
  114.  
  115.            
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement