/// <summary> /// Handle the exception /// </summary> /// <param name="ex">The exception to be handled</param> /// <param name="message">An additional message to be included in the error email body</param> public static void HandleException(Exception ex, string message) { if (!StringUtils.IsBlank(ToEmail)) { try { Email email = Email.Create(); email.FromEmail = FromEmail; foreach (string recipient in ToEmail.Split(';')) { if (StringUtils.IsEmail(recipient)) { email.Recipients.Add(recipient); } } email.Subject = Subject; email.Body = GetBody(ex, message); email.IsHtml = true; email.IsDebugMode = false; email.Send(); } catch (Exception hex) { m_Logger.Warn("Error handling error", hex); } } }
// Outgoing Mail (SMTP) Server - requires TLS or SSL: //smtp.gmail.com //Use Authentication: Yes //Port for TLS/STARTTLS: 587 //Port for SSL: 465 //Server timeouts Greater than 1 minute, we recommend 5 //Full Name or Display Name: [your name] //Account Name or User Name: your full email address (including @gmail.com or @your_domain.com) //Email Address: your email address ([email protected] or username@your_domain.com) //Password: your Gmail password public void Send() { bool isEmailEnabled = true; bool.TryParse(ConfigurationManager.AppSettings["EmailEnable"], out isEmailEnabled); if (!isEmailEnabled) { return; } SmtpClient smtp = new SmtpClient(); smtp.Host = GmailHost; smtp.Port = GmailPort; smtp.EnableSsl = GmailSSL; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword); var message = new MailMessage(); message.From = new MailAddress(GmailUsername); string[] ToMuliId = ToEmail.Split(','); foreach (string ToEMailId in ToMuliId) { message.To.Add(new MailAddress(ToEMailId)); //adding multiple TO Email Id } if (!string.IsNullOrEmpty(CC)) { string[] CCId = CC.Split(','); foreach (string CCEmail in CCId) { message.CC.Add(new MailAddress(CCEmail)); //Adding Multiple CC email Id } } //using (var message = new MailMessage(GmailUsername, ToEmail)) //{ message.Subject = Subject; message.Body = Body; message.IsBodyHtml = IsHtml; smtp.Send(message); //} }
// Outgoing Mail (SMTP) Server - requires TLS or SSL: //smtp.gmail.com //Use Authentication: Yes //Port for TLS/STARTTLS: 587 //Port for SSL: 465 //Server timeouts Greater than 1 minute, we recommend 5 //Full Name or Display Name: [your name] //Account Name or User Name: your full email address (including @gmail.com or @your_domain.com) //Email Address: your email address ([email protected] or username@your_domain.com) //Password: your Gmail password public void Send() { SmtpClient smtp = new SmtpClient(); smtp.Host = GmailHost; smtp.Port = GmailPort; smtp.EnableSsl = GmailSSL; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword); var message = new MailMessage(); message.From = new MailAddress(GmailUsername); string[] ToMuliId = ToEmail.Split(','); foreach (string ToEMailId in ToMuliId) { message.To.Add(new MailAddress(ToEMailId)); //adding multiple TO Email Id } string[] CCId = CC.Split(','); foreach (string CCEmail in CCId) { message.CC.Add(new MailAddress(CCEmail)); //Adding Multiple CC email Id } //using (var message = new MailMessage(GmailUsername, ToEmail)) //{ message.Subject = Subject; message.Body = Body; message.IsBodyHtml = IsHtml; smtp.Send(message); //} }
public string Send() { string strMsg = string.Empty; try { SmtpClient smtp = new SmtpClient(); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(MailUsername, MailPassword); smtp.Port = MailPort; smtp.Host = MailHost; smtp.EnableSsl = MailSSL; using (var message = new MailMessage()) { message.From = new MailAddress(MailUsername); foreach (string address in ToEmail.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { if (!string.IsNullOrEmpty(address)) { message.To.Add(address); } } foreach (string address in CCEmail.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { if (!string.IsNullOrEmpty(address)) { message.CC.Add(address); } } message.Subject = Subject; message.Body = Body; message.IsBodyHtml = IsHtml; if (FileName != null) { message.Attachments.Add(new Attachment(EmailAttachment, FileName)); } try { if (fileNameList.Count > 0) { for (int i = 0; i < fileNameList.Count; i++) { message.Attachments.Add(new Attachment(emailAttachmentsList[i], fileNameList[i])); } } } catch (Exception fileex) { strMsg = " " + fileex.ToString(); } smtp.Send(message); } strMsg = "Success"; } catch (Exception ex) { strMsg = "Fail : " + ex.ToString(); } return(strMsg); }