/// <summary> /// Sends an mail message /// </summary> /// <param name="from">Sender address</param> /// <param name="recepient">Recepient address</param> /// <param name="bcc">Bcc recepient</param> /// <param name="cc">Cc recepient</param> /// <param name="subject">Subject of mail message</param> /// <param name="body">Body of mail message</param> public static int Send_Email(string from, string recepient, List <string> bcc, string cc, string subject, string body, bool isBodyHtml) { // Instantiate a new instance of MailMessage Int16 I = default(Int16); MailMessage mMailMessage = new MailMessage(); try { // Set the sender address of the mail message mMailMessage.From = new MailAddress(from); // Set the recepient address of the mail message mMailMessage.To.Add(new MailAddress(recepient)); // Check if the bcc value is nothing or an empty string //if ((bcc != null) & bcc != string.Empty) if (bcc.Count > 0) { foreach (var bcc_email in bcc) { if (!string.IsNullOrEmpty(bcc_email.Trim()) && bcc_email.Contains("@")) { mMailMessage.Bcc.Add(new MailAddress(bcc_email)); } } } // Check if the cc value is nothing or an empty value //if ((cc != null) & cc != string.Empty) if (!string.IsNullOrEmpty(cc.Trim()) && cc.Contains("@")) { // Set the CC address of the mail message mMailMessage.CC.Add(new MailAddress(cc)); } // Set the subject of the mail message mMailMessage.Subject = subject; // Set the body of the mail message mMailMessage.Body = body; // Set the format of the mail message body as HTML mMailMessage.IsBodyHtml = isBodyHtml; // Set the priority of the mail message to normal mMailMessage.Priority = MailPriority.Normal; // Instantiate a new instance of SmtpClient SmtpClient mSmtpClient = new SmtpClient(); mSmtpClient.Host = strSMTPServer; //here it goes!!! //client.Port = 25; //163等都是默认25; //gmail 与QQ都是SSL 验证, 使用465端口 if (EnableSsl) { mSmtpClient.Port = EmailPort; //gmail ssl port mSmtpClient.EnableSsl = EnableSsl; } //Credentials if (!string.IsNullOrEmpty(strEmailUserName)) { mSmtpClient.Credentials = new System.Net.NetworkCredential(strEmailUserName, strEmailPassword); } // Send the mail message mSmtpClient.Send(mMailMessage); I = 0; mMailMessage = null; } catch (Exception ex) //Elmah.ApplicationException ex { Elmah.MemoryErrorLog log = new Elmah.MemoryErrorLog(); log.Log(new Elmah.Error(ex)); //这个只是记录到Elmah 内存中 Elmah.ErrorSignal.FromCurrentContext().Raise(ex); //这个才是手动记录; mMailMessage = null; I = 9; } return(I); }
/// <summary> /// Sends an mail message /// </summary> /// <param name="from">Sender address</param> /// <param name="recepient">Recepient address</param> /// <param name="bcc">Bcc recepient</param> /// <param name="cc">Cc recepient</param> /// <param name="subject">Subject of mail message</param> /// <param name="body">Body of mail message</param> public static int Send_Email(string from, string recepient, string bcc, string cc, string subject, string body, bool isBodyHtml) { // Instantiate a new instance of MailMessage Int16 I = default(Int16); MailMessage mMailMessage = new MailMessage(); try { // Set the sender address of the mail message mMailMessage.From = new MailAddress(from); // Set the recepient address of the mail message mMailMessage.To.Add(new MailAddress(recepient)); // Check if the bcc value is nothing or an empty string //if ((bcc != null) & bcc != string.Empty) if (!string.IsNullOrEmpty(bcc.Trim()) && bcc.Contains("@")) { // Set the Bcc address of the mail message mMailMessage.Bcc.Add(new MailAddress(bcc)); } // Check if the cc value is nothing or an empty value //if ((cc != null) & cc != string.Empty) if (!string.IsNullOrEmpty(cc.Trim()) && cc.Contains("@")) { // Set the CC address of the mail message mMailMessage.CC.Add(new MailAddress(cc)); } // Set the subject of the mail message mMailMessage.Subject = subject; // Set the body of the mail message mMailMessage.Body = body; // Set the format of the mail message body as HTML mMailMessage.IsBodyHtml = isBodyHtml; // Set the priority of the mail message to normal mMailMessage.Priority = MailPriority.Normal; // Instantiate a new instance of SmtpClient SmtpClient mSmtpClient = new SmtpClient(); mSmtpClient.Host = strSMTPServer; //Credentials if (!string.IsNullOrEmpty(strEmailUserName)) { mSmtpClient.Credentials = new System.Net.NetworkCredential(strEmailUserName, strEmailPassword); } // Send the mail message mSmtpClient.Send(mMailMessage); I = 0; mMailMessage = null; } catch (Exception ex) { Elmah.ErrorLog log = new Elmah.MemoryErrorLog(); log.Log(new Elmah.Error(ex)); mMailMessage = null; I = 9; //throw new ApplicationException(ex.Message); } return(I); }