/// <summary> ///Set Cc Address to the Mail /// </summary> /// <param name="mailMessage"></param> /// <param name="mailMsg"></param> private void SetCcAddress(System.Web.Mail.MailMessage mailMessage, MailMessage mailMsg) { try { //checking ccAddress string is null or empty if it not null then add to mail if (!String.IsNullOrEmpty(CcAddress)) { //split the ccAddress string and add to an array string[] ccArray = CcAddress.Split(";".ToCharArray()); ValidateRecipientEmail(ccArray);//validating the email address //if mail is net.mail if (IsSmtpClientMail) { foreach (string cc in ccArray.Where(cc => !cc.Equals(""))) { mailMsg.CC.Add(cc); //add each CcAddress in ccArray to mailmessage.cc } } else { //if mail is web.mail set ccAddress string to MailMessage.Cc CcAddress = CcAddress.TrimEnd(';'); mailMessage.Cc = CcAddress; } } else { //CcAddress is null then set CcFlag to 1 CcFlag = 1; } } catch (Exception ex) { throw ex; } }
public bool SendEmailToServer() { SmtpClient email = new SmtpClient { DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, EnableSsl = enableSsl, Host = host, Port = port, Credentials = new NetworkCredential(userName, password) }; string[] adrs = ToAddress.Split(';'); MailMessage msg = new MailMessage(userName, adrs[0]); msg.Subject = Subject; msg.Body = Body; if (adrs.Length > 1) { for (int i = 1; i < adrs.Length; i++) { msg.To.Add(adrs[i]); } } if (!string.IsNullOrEmpty(CcAddress)) { string[] adrscc = CcAddress.Split(';'); foreach (string adr in adrscc) { msg.To.Add(adr); } } foreach (string filename in attachments) { msg.Attachments.Add(new Attachment(filename)); } try { email.Send(msg); return(true); } catch (Exception ex) { return(false); } }