Exemplo n.º 1
0
 /// <summary>
 /// Email to one recipient.
 /// </summary>
 /// <param name="emailParameters"></param>
 /// <param name="to"></param>
 /// <param name="subject"></param>
 /// <param name="body"></param>
 /// <param name="isHtml"></param>
 /// <param name="attachmentFileList"></param>
 public static void Email(EmailParameters emailParameters, string to, string subject, string body, bool isHtml, IEnumerable<string> attachmentFileList)
 {
     Email(emailParameters, new string[] { to }, null, null, subject, body, isHtml, null);
 }
Exemplo n.º 2
0
        public static void Email(
            EmailParameters emailParameters,
            IEnumerable<string> toList, IEnumerable<string> ccList, IEnumerable<string> bccList,
            string subject, string body, bool isHtml,
            IEnumerable<string> attachmentFileList)
        {
            // Verification
            IEnumerator<string> enumerator = toList.GetEnumerator();
            if (toList == null || !enumerator.MoveNext() || string.IsNullOrEmpty(enumerator.Current))
            {
                throw new Exception("There must be at least one email address in the To list.");
            }

            foreach (string email in toList)
                if (!IsValidEmail(email))
                    throw new Exception("Invalid email in the To list: " + email);

            if (ccList != null)
            {
                foreach (string email in ccList)
                    if (!IsValidEmail(email))
                        throw new Exception("Invalid email in the Cc list: " + email);
            }

            if (bccList != null)
            {
                foreach (string email in bccList)
                    if (!IsValidEmail(email))
                        throw new Exception("Invalid email in the Bcc list: " + email);
            }

            // Smtp server
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = emailParameters.Host;
            if (!emailParameters.UsingDefaultPort)
            {
                smtpClient.Port = emailParameters.Port;
            }
            smtpClient.EnableSsl = emailParameters.UseSSL;
            smtpClient.Credentials = new System.Net.NetworkCredential(emailParameters.User, Util.Decrypt(emailParameters.Password));

            // Emails From, To, Cc, Bcc and attachments
            MailMessage mailMessage = new MailMessage();

            mailMessage.From = new MailAddress(emailParameters.Email, emailParameters.DisplayName);
            foreach (string to in toList)
            {
                mailMessage.To.Add(new MailAddress(to));
            }
            if (ccList != null)
            {
                foreach (string cc in ccList)
                {
                    mailMessage.CC.Add(new MailAddress(cc));
                }
            }
            if (bccList != null)
            {
                foreach (string bcc in bccList)
                {
                    mailMessage.Bcc.Add(new MailAddress(bcc));
                }
            }
            if (attachmentFileList != null)
            {
                foreach (string fileName in attachmentFileList)
                {
                    mailMessage.Attachments.Add(new Attachment(fileName));
                }
            }

            // Email Subject and Body
            mailMessage.Subject = subject;
            mailMessage.IsBodyHtml = isHtml;
            mailMessage.Body = body;

            // Send email
            if (!(Util.IsDebug() && emailParameters.SkipEmailOnDebug))
            {
                smtpClient.Send(mailMessage);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Email to one recipient.
 /// </summary>
 /// <param name="emailParameters"></param>
 /// <param name="to"></param>
 /// <param name="emailContent"></param>
 /// <param name="attachmentFileList"></param>
 public static void Email(EmailParameters emailParameters, string to, EmailContent emailContent, List<string> attachmentFileList)
 {
     Email(emailParameters, to, emailContent.Subject, emailContent.Body, emailContent.IsHtml, attachmentFileList);
 }