예제 #1
0
        public async Task <List <MSMessage> > ReplyToMessageAsync(string id, string content)
        {
            try
            {
                var(originMessage, threadId) = await this.GetMessageById(id);

                var reply = new MimeMessage();

                // reply to the sender of the message
                if (originMessage.ReplyTo.Count > 0)
                {
                    reply.To.AddRange(originMessage.ReplyTo);
                }
                else if (originMessage.From.Count > 0)
                {
                    reply.To.AddRange(originMessage.From);
                }
                else if (originMessage.Sender != null)
                {
                    reply.To.Add(originMessage.Sender);
                }

                // set the reply subject
                if (!originMessage.Subject.StartsWith(EmailCommonStrings.Reply, StringComparison.OrdinalIgnoreCase))
                {
                    reply.Subject = string.Format(EmailCommonStrings.ReplyReplyFormat, originMessage.Subject);
                }
                else
                {
                    reply.Subject = originMessage.Subject;
                }

                // construct the In-Reply-To and References headers
                if (!string.IsNullOrEmpty(originMessage.MessageId))
                {
                    reply.InReplyTo = originMessage.MessageId;
                    foreach (var mid in originMessage.References)
                    {
                        reply.References.Add(mid);
                    }

                    reply.References.Add(originMessage.MessageId);
                }

                // quote the original message text
                using (var quoted = new StringWriter())
                {
                    var sender = originMessage.Sender ?? originMessage.From.Mailboxes.FirstOrDefault();
                    quoted.WriteLine(EmailCommonStrings.EmailInfoFormat, originMessage.Date.ToString("f"), !string.IsNullOrEmpty(sender.Name) ? sender.Name : sender.Address);
                    using (var reader = new StringReader(originMessage.TextBody))
                    {
                        string line;

                        while ((line = reader.ReadLine()) != null)
                        {
                            quoted.Write("> ");
                            quoted.WriteLine(line);
                        }
                    }

                    content = quoted.ToString();
                }

                var sendRequest = service.Users.Messages.Send(
                    new GmailMessage()
                {
                    Raw      = Base64UrlEncode(reply.ToString() + content),
                    ThreadId = threadId,
                }, "me");
                await((IClientServiceRequest <GmailMessage>)sendRequest).ExecuteAsync();
                return(null);
            }
            catch (GoogleApiException ex)
            {
                throw GoogleClient.HandleGoogleAPIException(ex);
            }
        }
예제 #2
0
        public async Task <List <MSMessage> > GetMyMessagesAsync(DateTime fromTime, DateTime toTime, bool getUnRead = false, bool isImportant = false, bool directlyToMe = false, string fromAddress = null, int skip = 0)
        {
            try
            {
                var profileRequest = service.Users.GetProfile("me");
                var user           = ((IClientServiceRequest <Profile>)profileRequest).Execute();
                var userAddress    = user.EmailAddress;

                string searchOperation = string.Empty;
                searchOperation = this.AppendFilterString(searchOperation, "in:inbox");
                if (getUnRead)
                {
                    searchOperation = this.AppendFilterString(searchOperation, "is:unread");
                }

                if (isImportant)
                {
                    searchOperation = this.AppendFilterString(searchOperation, "is:important");
                }

                if (directlyToMe)
                {
                    searchOperation = this.AppendFilterString(searchOperation, $"deliveredto:{userAddress}");
                }

                if (fromAddress != null)
                {
                    searchOperation = this.AppendFilterString(searchOperation, $"from:{fromAddress}");
                }

                if (fromTime != null)
                {
                    searchOperation = this.AppendFilterString(searchOperation, $"after:{fromTime.Year}/{fromTime.Month}/{fromTime.Day}");
                }

                if (toTime != null)
                {
                    searchOperation = this.AppendFilterString(searchOperation, $"before:{toTime.Year}/{toTime.Month}/{toTime.Day}");
                }

                var request = service.Users.Messages.List("me");
                request.Q          = searchOperation;
                request.MaxResults = this.pageSize;

                // deal with skip
                if (skip != 0 && this.pageToken == string.Empty)
                {
                    // call api and get the pageToken
                    var tempReq = service.Users.Messages.List("me");
                    tempReq.MaxResults = skip;
                    tempReq.Q          = searchOperation;
                    var tempRes = ((IClientServiceRequest <ListMessagesResponse>)tempReq).Execute();
                    if (tempRes.NextPageToken != null && tempRes.NextPageToken != string.Empty)
                    {
                        this.pageToken = tempRes.NextPageToken;
                    }
                    else
                    {
                        // no more message
                        return(new List <MSMessage>());
                    }

                    request.PageToken = this.pageToken;
                }

                var response = await((IClientServiceRequest <ListMessagesResponse>)request).ExecuteAsync();
                var result   = new List <MSMessage>();

                // response.Messages only have id and threadID
                if (response.Messages != null)
                {
                    var messages = await Task.WhenAll(response.Messages.Select(temp =>
                    {
                        var req    = service.Users.Messages.Get("me", temp.Id);
                        req.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Raw;
                        return(((IClientServiceRequest <GmailMessage>)req).ExecuteAsync());
                    }));

                    if (messages != null && messages.Length > 0)
                    {
                        foreach (var m in messages)
                        {
                            // map to msgraph email
                            var ms = this.MapMimeMessageToMSMessage(DecodeToMessage(m.Raw));
                            ms.BodyPreview = m.Snippet;
                            ms.Id          = m.Id;
                            ms.WebLink     = $"https://mail.google.com/mail/#inbox/{m.Id}";
                            result.Add(ms);
                        }
                    }
                }

                if (response.NextPageToken != null && response.NextPageToken != string.Empty)
                {
                    this.pageToken = response.NextPageToken;
                }
                else
                {
                    this.pageToken = string.Empty;
                }

                return(result);
            }
            catch (GoogleApiException ex)
            {
                throw GoogleClient.HandleGoogleAPIException(ex);
            }
        }
예제 #3
0
        /// <inheritdoc/>
        public async Task ForwardMessageAsync(string id, string content, List <Recipient> recipients)
        {
            try
            {
                // getOriginalMessage
                var(originalMessage, threadId) = await this.GetMessageById(id);

                var forward = new MimeMessage();
                foreach (var recipient in recipients)
                {
                    forward.To.Add(new MailboxAddress(recipient.EmailAddress.Address));
                }

                // set the reply subject
                forward.Subject = string.Format(EmailCommonStrings.ForwardReplyFormat, originalMessage.Subject);

                // construct the References headers
                foreach (var mid in originalMessage.References)
                {
                    forward.References.Add(mid);
                }

                if (!string.IsNullOrEmpty(originalMessage.MessageId))
                {
                    forward.References.Add(originalMessage.MessageId);
                }

                // quote the original message text
                using (var quoted = new StringWriter())
                {
                    var sender = originalMessage.Sender ?? originalMessage.From.Mailboxes.FirstOrDefault();
                    quoted.WriteLine(content);
                    quoted.WriteLine();
                    quoted.WriteLine(EmailCommonStrings.ForwardMessage);
                    quoted.WriteLine(EmailCommonStrings.FromFormat, originalMessage.From);
                    quoted.WriteLine(EmailCommonStrings.DateFormat, originalMessage.Date);
                    quoted.WriteLine(EmailCommonStrings.SubjectFormat, originalMessage.Subject);
                    quoted.WriteLine(EmailCommonStrings.ToFormat, originalMessage.To);
                    if (originalMessage.Cc.Count > 0)
                    {
                        quoted.WriteLine(EmailCommonStrings.CCFormat, originalMessage.Cc);
                    }

                    using (var reader = new StringReader(originalMessage.TextBody))
                    {
                        string line;

                        while ((line = reader.ReadLine()) != null)
                        {
                            quoted.Write("> ");
                            quoted.WriteLine(line);
                        }
                    }

                    content = quoted.ToString();
                }

                var sendRequest = service.Users.Messages.Send(
                    new GmailMessage()
                {
                    Raw      = Base64UrlEncode(forward.ToString() + content),
                    ThreadId = threadId,
                }, "me");
                await((IClientServiceRequest <GmailMessage>)sendRequest).ExecuteAsync();
            }
            catch (GoogleApiException ex)
            {
                throw GoogleClient.HandleGoogleAPIException(ex);
            }
        }