public async void Check(MailService i_service) { var t_lastVid = ""; try { var t_post = m_feed.Items.FirstOrDefault(); foreach (SyndicationItem l_item in m_feed.Items.Reverse()) { if (l_item.PublishDate > LastNotice) { var t_url = l_item.Links.First().Uri.ToString(); MailRequest t_mail = new MailRequest { ToEmail = "*****@*****.**", Subject = $"{l_item.Authors.First().Name} - {l_item.Title.Text}", Body = t_url, }; t_lastVid = t_mail.Subject; await i_service.SendEmailAsync(t_mail); LastNotice = l_item.PublishDate.UtcDateTime; Console.WriteLine("Sending: " + t_mail.Subject); } } } catch (Exception) { Console.WriteLine("Error while sending: " + t_lastVid); } }
public async Task SendEmailAsync(MailRequest mailRequest) { MailMessage message = new MailMessage(); SmtpClient smtp = new SmtpClient(); message.From = new MailAddress(_mailSettings.Mail, _mailSettings.DisplayName); message.To.Add(new MailAddress(mailRequest.ToEmail)); message.Subject = mailRequest.Subject; message.IsBodyHtml = false; message.Body = mailRequest.Body; smtp.Port = _mailSettings.Port; smtp.Host = _mailSettings.Host; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(_mailSettings.Mail, _mailSettings.Password); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; await smtp.SendMailAsync(message); }