public async Task SendEmail(EmailForCreationDto emailForCreationDto)
        {
            try
            {
                MailMessage message = new MailMessage();

                message.To.Add(new MailAddress("*****@*****.**"));         // Wiadomosc DO (odbiorca)
                message.From     = new MailAddress("*****@*****.**");  // OD. Mail stworzony tylko na potrzeby wysylania formualrza (nadawca)
                message.Subject  = emailForCreationDto.Topic.ToString();
                message.Body     = emailForCreationDto.Content.ToString();
                message.Priority = MailPriority.High;

                if (emailForCreationDto.File.Length > 0)
                {
                    using (var stream = emailForCreationDto.File.OpenReadStream())
                    {
                        message.Attachments.Add(new Attachment(stream, "zdjecie"));

                        using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))                                    // serwer smtp i port dla Gmail
                        {
                            smtp.UseDefaultCredentials = false;
                            smtp.Credentials           = new NetworkCredential("*****@*****.**", "agrostryszawa"); // poswiadczeniam haslo moze byc zle, stare jest na starym commicie
                            smtp.EnableSsl             = true;

                            await smtp.SendMailAsync(message);
                        }
                    }
                    emailForCreationDto.IsPhoto = true;                         // jezeli znajduje sie zdjecie w wiadomosci dolaczana jest flaga true
                }

                var EmailToReturn = _mapper.Map <Email>(emailForCreationDto);
                _context.Emails.Add(EmailToReturn);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                await _context.SaveChangesAsync();
            }
        }
Пример #2
0
        public async Task <IActionResult> SendEmail([FromForm] EmailForCreationDto emailForCreationDto)
        {
            await _emailRepository.SendEmail(emailForCreationDto);

            return(Ok());
        }