private void SendMail(IEnumerable <Employees> bday, MailDetails mailDetails) { try { MailMessage msg = new MailMessage(); msg.From = new MailAddress(mailDetails.User, "Chris"); msg.To.Add(new MailAddress("*****@*****.**", "Chris")); msg.Subject = "Birthday Reminder"; var toBday = "Tomorrow's Birthday People: "; foreach (var person in bday) { toBday = toBday + "\n" + person.FirstName + " " + person.LastName; } msg.Body = toBday; var SenderEmail = mailDetails.User; var SenderEmailPassword = mailDetails.Pass; StringBuilder str = new StringBuilder(); str.AppendLine("BEGIN:VCALENDAR"); str.AppendLine("PRODID:-//Schedule a Meeting"); str.AppendLine("VERSION:2.0"); str.AppendLine("METHOD:REQUEST"); str.AppendLine("BEGIN:VEVENT"); str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmss}", DateTime.Today.AddDays(1).ToUniversalTime() - new TimeSpan(0, 0, 1))); str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmss}", DateTime.Today.ToUniversalTime())); str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmss}", DateTime.Today.AddDays(2).ToUniversalTime())); str.AppendLine("LOCATION: " + "Burbank, CA"); str.AppendLine(string.Format("UID:{0}", Guid.NewGuid())); str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body)); str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body)); str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject)); str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address)); str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address)); str.AppendLine("BEGIN:VALARM"); str.AppendLine("TRIGGER:-PT15M"); str.AppendLine("ACTION:DISPLAY"); str.AppendLine("DESCRIPTION:Reminder"); str.AppendLine("END:VALARM"); str.AppendLine("END:VEVENT"); str.AppendLine("END:VCALENDAR"); System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient(); smtpclient.Host = mailDetails.Host; //-------this has to given the Mailserver IP smtpclient.Port = mailDetails.Port; smtpclient.EnableSsl = true; smtpclient.Credentials = new System.Net.NetworkCredential(SenderEmail.Trim(), SenderEmailPassword.Trim()); System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar"); contype.Parameters.Add("method", "REQUEST"); contype.Parameters.Add("name", "Meeting.ics"); AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype); msg.AlternateViews.Add(avCal); smtpclient.Send(msg); } catch (Exception) { _logger.LogInformation("Send Email Failed."); } }
public TimedHostedService(IServiceScopeFactory scopeFactory, ILogger <TimedHostedService> logger, IOptions <MailDetails> mailDetailing) { this.scopeFactory = scopeFactory; _logger = logger; _mailDetails = mailDetailing.Value; }