private List <Personalization> BuildToRecipients() { var addressesTo = EmailTo?.Split(';'); var addressesBcc = EmailBCC?.Split(';'); var addressesCc = EmailCC?.Split(';'); var personalizationList = new List <Personalization>(); foreach (var address in addressesTo) { var personalization = new Personalization { Tos = new List <EmailAddress> { new EmailAddress() { Email = address } } }; if (addressesBcc != null) { foreach (var bcc in addressesBcc) { personalization.Bccs = new List <EmailAddress> { new EmailAddress() { Email = bcc } }; } } if (addressesCc != null) { foreach (var cc in addressesCc) { personalization.Ccs = new List <EmailAddress> { new EmailAddress() { Email = cc } }; } } personalizationList.Add(personalization); } return(personalizationList); }
public Requests.EmailSend ToObject() { return(new Requests.EmailSend { Account = Account, AttachmentPaths = AttachmentPaths?.Split(','), EmailCC = EmailCC?.Split(','), EmailFrom = EmailFrom, EmailTo = EmailTo?.Split(','), Message = Message, Mime = Mime, Subject = Subject }); }
public void SendEmail() { var message = new MimeMessage(); // Add Email From message.From.Add(new MailboxAddress(EmailFrom)); // Add Email To if (EmailTo != null && EmailTo != String.Empty) { if (EmailTo.Contains(";")) { List <string> toList = new List <string>(); toList = EmailTo.Split(';').ToList(); foreach (string email in toList) { message.To.Add(new MailboxAddress(email)); } } else { message.To.Add(new MailboxAddress(EmailTo)); } } // Add Email CC if (EmailCC != null && EmailCC != String.Empty) { if (EmailCC.Contains(";")) { List <string> toList = new List <string>(); toList = EmailCC.Split(';').ToList(); foreach (string email in toList) { message.Cc.Add(new MailboxAddress(email)); } } else { message.Cc.Add(new MailboxAddress(EmailCC)); } } // Add Subject, currently just an error subject message.Subject = EmailSubject; // Add Body, currently just an error body message.Body = new TextPart("plain") { Text = EmailBody }; using (var client = new SmtpClient(new ProtocolLogger("smtp.log"))) { int count = 0; int maxTries = 1000; bool done = false; while (done == false) { try { UpdateConsoleStep("Attempting to Connect to SMTP Server..."); client.Disconnect(true); client.Timeout = 60000; client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.CheckCertificateRevocation = false; //client.SslProtocols = System.Security.Authentication.SslProtocols.Tls; client.Connect(EmailConfig.SmtpServer, EmailConfig.SmtpPort, MailKit.Security.SecureSocketOptions.Auto); done = true; } catch (Exception e) { UpdateConsoleStep(e.Message); client.Disconnect(true); if (e.Message.Contains("existing connection was forcibly closed")) { if (++count == maxTries) { return; } else { System.Threading.Thread.Sleep(500); } } } } UpdateConsoleStep("Authenticating User to SMTP Server..."); client.Authenticate(EmailConfig.SmtpUsername, EmailConfig.SmtpPassword); UpdateConsoleStep("Sending Email..."); client.Send(message); UpdateConsoleStep("Disconnecting from SMTP Server..."); client.Disconnect(true); } }
static int LoadEmail(bool ProductionOnly, string[] args, SqlCommand cmd2, DateTime CurrentDateTime, string EmailIdentifier) { int Status = 0; // Create a database connection SqlConnection conn = new SqlConnection("Data Source=(local);Integrated Security=SSPI;Initial Catalog=EATDW;Connection Timeout=120"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); // Retrieve emails from the database. cmd.CommandText = "Select Top 900 SendEmailId,EmailTo,EmailCC,EmailSubject,EmailBodyText,EmailBodyDataTableHeader,EmailBodyDataTable,EmailBodyDataTableFooter From Email_SendEmail_Archive Where SendEmailType = '" + args[0] + "' And SendEmailLevelName = '" + args[1] + "' And SendAttemptDateTime = '" + CurrentDateTime + "' And SendStatus = 'Not Sent' And SummarySegment = '" + args[3] + "'"; SqlDataReader reader = cmd.ExecuteReader(); String SendEmailId = ""; String EmailTo; String EmailCC; String EmailSubject; String EmailBody; String EmailBodyText; String EmailBodyDataTableHeader; String EmailBodyDataTable; String EmailBodyDataTableFooter; try { while (reader.Read()) { SendEmailId = reader["SendEmailId"].ToString(); EmailTo = reader["EmailTo"].ToString(); EmailCC = reader["EmailCC"].ToString(); EmailSubject = reader["EmailSubject"].ToString(); EmailBodyText = reader["EmailBodyText"].ToString(); EmailBodyDataTableHeader = reader["EmailBodyDataTableHeader"].ToString(); EmailBodyDataTable = reader["EmailBodyDataTable"].ToString(); EmailBodyDataTableFooter = reader["EmailBodyDataTableFooter"].ToString(); // Prep the email for sending EmailTo = EmailTo.Replace(";", "@microsoft.com,"); if (EmailTo != "") { EmailTo = EmailTo.Substring(0, EmailTo.Length - 1); } EmailCC = EmailCC.Replace(";", "@microsoft.com,"); if (EmailCC != "") { EmailCC = EmailCC.Substring(0, EmailCC.Length - 1); } EmailBody = EmailBodyText; if (!ProductionOnly) { EmailBody = EmailBody.Replace("<tr><td><img src=\"http://annuity/emailbanner.jpg\"/><br/></td></tr>", "<tr><td>To: [EmailTo]</td></tr><tr><td>CC: [EmailCC]</td></tr><tr><td><img src=\"http://annuity/emailbanner.jpg\"/><br/></td></tr>"); EmailBody = EmailBody.Replace("[EmailTo]", EmailTo); EmailBody = EmailBody.Replace("[EmailCC]", EmailCC); } EmailBody = EmailBody.Replace("[EmailBodyDataTableHeader]", EmailBodyDataTableHeader); EmailBody = EmailBody.Replace("[EmailBodyDataTable]", EmailBodyDataTable); EmailBody = EmailBody.Replace("[EmailBodyDataTableFooter]", EmailBodyDataTableFooter); EmailBody = EmailBody.Replace("[EmailIdentifier]", EmailIdentifier); //Console.WriteLine("done setting parameters for: " + SendEmailId);//test //Console.WriteLine("EmailTo:" + EmailTo);//test //Console.WriteLine("EmailCC:" + EmailCC);//test // Send Email try { SendEmail(EmailTo, EmailCC, EmailSubject, EmailBody, SendEmailId, ProductionOnly); //Console.WriteLine("done sending email for: " + SendEmailId);//test cmd2.CommandType = CommandType.Text; cmd2.CommandText = "Update Email_SendEmail_Archive Set SendStatus = 'Success' Where SendEmailId = " + SendEmailId + " And SendAttemptDateTime = '" + CurrentDateTime + "'"; } catch (Exception ex) { String ErrorMessage = ex.Message; //Console.WriteLine(ErrorMessage);//test //Console.ReadLine();//test cmd2.CommandType = CommandType.Text; cmd2.CommandText = "Update Email_SendEmail_Archive Set SendStatus = 'Fail - App Error' Where SendEmailId = " + SendEmailId + " And SendAttemptDateTime = '" + CurrentDateTime + "'"; Status = 1; } cmd2.ExecuteNonQuery(); //Console.WriteLine("done setting email send status for: " + SendEmailId);//test } } catch (Exception ex2) { String ErrorMessage = ex2.Message; //Console.WriteLine(ErrorMessage);//test //Console.ReadLine();//test cmd2.CommandType = CommandType.Text; cmd2.CommandText = "Update Email_SendEmail_Archive Set SendStatus = 'Fail - App Error' Where SendEmailId = " + SendEmailId + " And SendAttemptDateTime = '" + CurrentDateTime + "'"; cmd2.ExecuteNonQuery(); Status = 1; //Console.WriteLine("done setting email app failure for: " + SendEmailId);//test } reader.Close(); conn.Close(); return(Status); }