public static void SetMailboxesFromStructure(this InternetAddressList mails, Structure newMails, string nameOfEntity)
 {
     mails.Clear();
     if (newMails is TextStructure text)
     {
         var mailsArray = text.Value.Split(new char[] { ';', SpecialChars.ArraySeparator[0] });
         foreach (var mail in mailsArray)
         {
             if (!string.IsNullOrEmpty(mail))
             {
                 mails.Add(MailboxAddress.Parse(mail));
             }
         }
     }
     else if (newMails is ListStructure list)
     {
         foreach (var mail in list.Value)
         {
             if (!string.IsNullOrEmpty(mail?.ToString()))
             {
                 mails.Add(MailboxAddress.Parse(mail.ToString()));
             }
         }
     }
     else
     {
         throw new ArgumentException($"'{nameOfEntity}' should be text or list structure");
     }
 }
        public virtual async Task RunAsync(CancellationToken cancellationToken)
        {
            var to  = new InternetAddressList();
            var cc  = new InternetAddressList();
            var bcc = new InternetAddressList();

            var mailSender = MailMergeService.Sender; // there's always a new instance returned!

            var onBeforeSend = new EventHandler <MailSenderBeforeSendEventArgs>((sender, args) =>
            {
                to.AddRange(args.MimeMessage.To);
                cc.AddRange(args.MimeMessage.Cc);
                bcc.AddRange(args.MimeMessage.Bcc);
            });

            mailSender.OnBeforeSend += onBeforeSend;

            try
            {
                if (MailData is IEnumerable <object> mailData)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    await mailSender.SendAsync <object>(MailMessage, mailData);
                }
                else
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    await mailSender.SendAsync(MailMessage, MailData);
                }

                Logger.LogInformation($"Mail sent. TO: {to} - CC: {cc} - BCC: {bcc}");
            }
            catch (Exception e) when(e is TaskCanceledException || e is OperationCanceledException)
            {
                Logger.LogError(e, "Mail failure. TO: {to} - CC: {cc} - BCC: {bcc}\nSubject: {subject}\nMessage: {message}", to, cc, bcc, MailMessage.Subject, MailMessage.PlainText);
                mailSender.SendCancel();
                throw;
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Mail failure. TO: {to} - CC: {cc} - BCC: {bcc}\nSubject: {subject}\nMessage: {message}", to, cc, bcc, MailMessage.Subject, MailMessage.PlainText);
                throw;
            }
            finally
            {
                MailMergeService.Sender.OnBeforeSend -= onBeforeSend;
                to.Clear();
                cc.Clear();
                bcc.Clear();
            }
        }
Beispiel #3
0
		void ReloadAddressList (HeaderId id, InternetAddressList list)
		{
			// clear the address list and reload
			list.Changed -= InternetAddressListChanged;
			list.Clear ();

			foreach (var header in Headers) {
				if (header.Id != id)
					continue;

				int length = header.RawValue.Length;
				List<InternetAddress> parsed;
				int index = 0;

				if (!InternetAddressList.TryParse (Headers.Options, header.RawValue, ref index, length, false, false, out parsed))
					continue;

				list.AddRange (parsed);
			}

			list.Changed += InternetAddressListChanged;
		}