public void EnviarCorreo() { try { mail.From = De.Trim(); if (Para != null) { mail.To = Para.Trim(); } if (Cc != null) { mail.To = Cc.Trim(); } mail.Subject = Asunto; mail.Priority = MailPriority.High; mail.BodyFormat = MailFormat.Html; mail.Body = Contenido; SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings["servidorMail"].ToString(); SmtpMail.Send(mail); } catch (Exception ex) { throw (ex); } }
public void SetMessageCc() { Cc = Cc?.Trim().TrimEnd(','); }
/// <summary> /// Creates a Mail object and sends the email if the SMTPServer variable is not /// empty or null /// </summary> /// <param name="timesToRetry">Number of retries</param> /// <param name="retryTimeout">Miliseconds between each retry.</param> public void SendEmail(int timesToRetry = 0, int retryTimeout = 5000) { // Create Mail object var oMailMessage = new MailMessage(); try { // Set properties needed for the email oMailMessage.From = new MailAddress(From); //oMailMessage.To.Add(new MailAddress(To)); var splitChar = To.Contains(",") ? "," : ";"; var emTo = To.Trim().Split(splitChar.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (emTo.Length == 0) { throw new ArgumentException("Argument 'To' must be a valid email repository, please review It."); } foreach (string toAdd in emTo) { oMailMessage.To.Add(toAdd); } if (!string.IsNullOrEmpty(Cc)) { //oMailMessage.CC.Add(new MailAddress(Cc)); splitChar = Cc.Contains(",") ? "," : ";"; var emCc = Cc.Trim().Split(splitChar.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (emCc.Length == 0) { throw new ArgumentException("Argument 'Cc' must be a valid email repository, please review It."); } foreach (string CcAdd in emCc) { //oMailMessage.To.Add(CcAdd); oMailMessage.CC.Add(CcAdd); } } oMailMessage.Subject = ((string.IsNullOrEmpty(Subject) ? "" : Subject)); oMailMessage.Body = ((string.IsNullOrEmpty(Body) ? "" : Body)); oMailMessage.IsBodyHtml = IsBodyHtml; if (Attachments != null && Attachments.Count > 0) { foreach (string attachment in Attachments) { var mailAttachment = new Attachment(attachment.Trim()); oMailMessage.Attachments.Add(mailAttachment); } } //Utility.RetryAction(() => _smtpClient.Send(oMailMessage), timesToRetry, retryTimeout); Utility.RetryMethod(new Action <MailMessage>(_smtpClient.Send), timesToRetry, retryTimeout, oMailMessage); } finally { //destroy the oMailMessage object to release attachements from usage by the process oMailMessage.Dispose(); } }