Ejemplo n.º 1
0
        /// <summary>
        /// Creates a custom "from" send-as alias. If an SMTP MSA is specified, Gmail will attempt to connect to the SMTP service to validate the configuration before creating the alias. If ownership verification is required for the alias, a message will be sent to the email address and the resource's verification status will be set to pending; otherwise, the resource will be created with verification status set to accepted. If a signature is provided, Gmail will sanitize the HTML before saving it with the alias.
        /// Documentation https://developers.google.com/gmail/v1/reference/sendAs/create
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated gmail service.</param>
        /// <param name="userId">User's email address. The special value "me" can be used to indicate the authenticated user.</param>
        /// <param name="body">A valid gmail v1 body.</param>
        /// <returns>SendAsResponse</returns>
        public static SendAs Create(gmailService service, string userId, SendAs body)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (body == null)
                {
                    throw new ArgumentNullException("body");
                }
                if (userId == null)
                {
                    throw new ArgumentNullException(userId);
                }

                // Make the request.
                return(service.SendAs.Create(body, userId).Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request SendAs.Create failed.", ex);
            }
        }
Ejemplo n.º 2
0
        public void AddSendAs(string id, SendAs sendAs)
        {
            id.ThrowIfNotEmailAddress();

            GmailService service = this.GetService(id);

            service.Users.Settings.SendAs.Create(sendAs, id).ExecuteWithRetry(RetryEvents.BackoffOAuthNotFound, this.RetryCount, 100);
        }
        private AttributeChange ApplySendAsChanges(CSEntryChange csentry)
        {
            this.GetUserSendAsChanges(csentry, out IList <string> adds, out IList <string> deletes);

            if (adds.Count == 0 && deletes.Count == 0)
            {
                return(null);
            }

            AttributeChange     change       = null;
            IList <ValueChange> valueChanges = new List <ValueChange>();

            try
            {
                if (deletes != null)
                {
                    foreach (string delete in deletes)
                    {
                        Logger.WriteLine($"Removing send as address {delete}");
                        MailAddress address = new MailAddress(delete);
                        this.config.GmailService.RemoveSendAs(csentry.DN, address.Address);
                        valueChanges.Add(ValueChange.CreateValueDelete(delete));
                    }
                }

                foreach (string add in adds)
                {
                    Logger.WriteLine($"Adding send as address {add}");

                    MailAddress address = new MailAddress(add);
                    SendAs      sendAs  = new SendAs
                    {
                        DisplayName = address.DisplayName,
                        SendAsEmail = address.Address,
                    };

                    if (this.config.MakeNewSendAsAddressesDefault)
                    {
                        sendAs.IsDefault = true;
                    }

                    this.config.GmailService.AddSendAs(csentry.DN, sendAs);
                    valueChanges.Add(ValueChange.CreateValueAdd(add));
                }
            }
            finally
            {
                if (valueChanges.Count > 0)
                {
                    if (csentry.ObjectModificationType == ObjectModificationType.Update)
                    {
                        change = AttributeChange.CreateAttributeUpdate(this.attributeName, valueChanges);
                    }
                    else
                    {
                        change = AttributeChange.CreateAttributeAdd(this.attributeName, valueChanges.Where(u => u.ModificationType == ValueModificationType.Add).Select(t => t.Value).ToList());
                    }
                }
            }

            return(change);
        }