public async Task EmailDetailsAsync(string to, string subject) { var appsettings = ConfigurationManager.AppSettings; MailMessage email = new MailMessage(new MailAddress(appsettings["Smtp.DefaultFromAddress"], appsettings["Smtp.DefaultFromUser"]), new MailAddress(to)); email.CC.Add(new MailAddress(appsettings["EmailTo"])); email.Subject = subject; email.IsBodyHtml = true; string message = "<html><body><table>"; //iterate over payment properties and add name and value to email message foreach (var prop in this.GetType().GetProperties()) { message += "<tr><td>" + prop.Name + "</td><td>" + prop.GetValue(this, null) + "</td></tr>"; } message += "</table></body></html>"; email.Body = message; using (var mailClient = new SmtpService()) { await mailClient.SendMailAsync(email); } }
static void Main(string[] args) { var services = new ServiceCollection(); services.AddOptions() .Configure <SmtpOption>(opt => { opt.Host = "smtp.gmail.com"; opt.Port = 465; opt.UseSsl = true; opt.UserName = "******"; opt.Password = "******"; opt.QuitAfterSending = true; }); var provider = services.BuildServiceProvider(); var optionSnapshot = provider.GetRequiredService <IOptionsSnapshot <SmtpOption> >(); var message1 = new MimeMessage() .AddFrom("Trung Tran", "...") .AddTo("Another Trung Tran", "...") .Subject("You will be OK?") .Body(html: "<div style='color:green;font-weight:bold'>It's OK</div>", SimpleAttachment.From(@"\somefile1"), SimpleAttachment.From(@"\somefile2")); var message2 = new MimeMessage() .AddFrom("Trung Tran", "...") .AddTo("Another Trung Tran", "...") .Subject("You will be OK? (twice)") .Body(html: "<div style='color:green;font-weight:bold'>It's OK</div>"); ISmtpService smtpService = new SmtpService(optionSnapshot); smtpService.SendMailAsync(message1, message2).Wait(); }