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);
        }
示例#2
0
 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);
            }
        }