Ejemplo n.º 1
0
        public async Task <Models.MailMessage[]> ReceiveAsync()
        {
            try
            {
                using Pop3Client emailClient = new Pop3Client();

                await emailClient.ConnectAsync(_serverAddress, _port, false).ConfigureAwait(false);

                //emailClient.AuthenticationMechanisms.Add("USER/PASS");
                await emailClient.AuthenticateAsync(_username, _password);

                int totalMessages = await emailClient.GetMessageCountAsync().ConfigureAwait(false);

                IList <Models.MailMessage> emails = new List <Models.MailMessage>();

                for (int i = 0; i < 50 && i < totalMessages; i++)
                {
                    MimeMessage message = await emailClient.GetMessageAsync(i).ConfigureAwait(false);

                    emails.Add(new Models.MailMessage()
                    {
                        Body    = !string.IsNullOrEmpty(message.HtmlBody) ? message.HtmlBody : message.TextBody,
                        Subject = message.Subject,
                        To      = message.To.Select(x => (MailboxAddress)x).Select(x => x.Address).First(),
                        From    = message.From.Select(x => (MailboxAddress)x).Select(x => x.Address).First()
                    });
                }

                return(emails.ToArray());
            }
            catch { }

            return(null);
        }
Ejemplo n.º 2
0
        public async Task <ApiResponse> ReceiveMailPopAsync(int min = 0, int max = 0)
        {
            using (var emailClient = new Pop3Client())
            {
                try
                {
                    await emailClient.ConnectAsync(_emailConfiguration.PopServer, _emailConfiguration.PopPort).ConfigureAwait(false);     // omitting usessl to allow mailkit to autoconfigure

                    emailClient.AuthenticationMechanisms.Remove("XOAUTH2");

                    if (!String.IsNullOrWhiteSpace(_emailConfiguration.PopUsername))
                    {
                        await emailClient.AuthenticateAsync(_emailConfiguration.PopUsername, _emailConfiguration.PopPassword).ConfigureAwait(false);
                    }

                    List <EmailMessageDto> emails = new List <EmailMessageDto>();

                    if (max == 0)
                    {
                        max = await emailClient.GetMessageCountAsync();           // if max not defined, get all messages
                    }
                    for (int i = min; i < max; i++)
                    {
                        var message = await emailClient.GetMessageAsync(i);

                        var emailMessage = new EmailMessageDto
                        {
                            Body    = !string.IsNullOrEmpty(message.HtmlBody) ? message.HtmlBody : message.TextBody,
                            IsHtml  = !string.IsNullOrEmpty(message.HtmlBody) ? true : false,
                            Subject = message.Subject
                        };
                        emailMessage.ToAddresses.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new EmailAddressDto(x.Name, x.Address)));
                        emailMessage.FromAddresses.AddRange(message.From.Select(x => (MailboxAddress)x).Select(x => new EmailAddressDto(x.Name, x.Address)));

                        emails.Add(emailMessage);
                    }

                    await emailClient.DisconnectAsync(true);

                    return(new ApiResponse(200, null, emails));
                }
                catch (Exception ex)
                {
                    return(new ApiResponse(500, ex.Message));
                }
            }
        }