/// <summary> /// Set To Address to the Mail /// </summary> /// <param name="mailMessage"></param> /// <param name="mailMsg"></param> private void SetToaddress(System.Web.Mail.MailMessage mailMessage, MailMessage mailMsg) { try { //checking toAddress string is null or empty if it not null then add to mail if (!String.IsNullOrEmpty(ToAddress)) { //split the toAddress string and add to an array string[] toArray = ToAddress.Split(";".ToCharArray()); ValidateRecipientEmail(toArray);//validating the email address //if mail is net.mail if (IsSmtpClientMail) { foreach (string to in toArray.Where(to => !to.Equals(""))) { mailMsg.To.Add(to); //add toAddress array to mailmessage.To } } else { //if mail is web.mail set toAddress string to MailMessage.To ToAddress = ToAddress.TrimEnd(';'); mailMessage.To = ToAddress; //multiple to address } } else { //Toaddress is null the set ToFlag=1 ToFlag = 1; } } catch (Exception ex) { throw ex; } }
/// <summary> /// Set Bcc Address to the Mail /// </summary> /// <param name="mailMessage"></param> /// <param name="mailMsg"></param> private void SetBccAddress(System.Web.Mail.MailMessage mailMessage, MailMessage mailMsg) { try { //checking bccAddress string is null or empty if it not null then add to mail if (!String.IsNullOrEmpty(BccAddress)) { //split the bccAddress string and add to an array string[] bccArray = BccAddress.Split(";".ToCharArray()); ValidateRecipientEmail(bccArray);//validating the email address //if mail is net.mail if (IsSmtpClientMail) { foreach (string bcc in bccArray.Where(bcc => !bcc.Equals(""))) { mailMsg.Bcc.Add(bcc); //add bccAddress array to mailmessage.Bcc } } else { //if mail is web.mail set bccAddress string to MailMessage.Bcc BccAddress = BccAddress.TrimEnd(';'); mailMessage.Bcc = BccAddress; } } else { //bccAddress is null then set BccFlag to 1 BccFlag = 1; } } catch (Exception ex) { throw ex; } }
/// <summary> /// Send Multiple or Single Attachments /// </summary> /// <param name="mailMessage"></param> /// <param name="mailMsg"></param> private void SetAttachments(System.Web.Mail.MailMessage mailMessage, MailMessage mailMsg) { try { //Send Multiple Attachments with mail /* //if (IsWindowsApplication) // For Window application * { * foreach (string fileName in AttachmentFileNames) * { * mailMsg.Attachments.Add(new Attachment(fileName)); * } * }*/ //else // for Web application { foreach (HttpPostedFile file in Attachments) { int attachmentFileLength = file.ContentLength; if (file != null && file.FileName != "" && attachmentFileLength > 0) //Check File name Exist { string strFileName = Path.GetFileName(file.FileName); file.SaveAs(HttpContext.Current.Server.MapPath(strFileName)); //Check mail is System.net.mail or System.web.mail if (IsSmtpClientMail) { //if it net.mail add multiple attachments to net.mail.mailmessage Attachment attachment = new Attachment(HttpContext.Current.Server.MapPath(strFileName)); mailMsg.Attachments.Add(attachment); } else { //web.mail add multiple mailattachments to web.mail.mailmessage MailAttachment attachment = new MailAttachment(HttpContext.Current.Server.MapPath(strFileName)); mailMessage.Attachments.Add(attachment); } _fileArray.Add(HttpContext.Current.Server.MapPath(strFileName)); } } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Over loaded Function sendmail with System.web.mail.mailMessage type argument /// </summary> /// <param name="mailMessage">mailmessage for web mail</param> /// <returns>bool value</returns> private bool SendMail(System.Web.Mail.MailMessage mailMessage) { try { mailMessage.Subject = Subject; //set subject to mailmessage if (ValidateEmailAddress(FromAddress)) { mailMessage.From = FromAddress; //if it not null set from address to mailmessage } else { throw new ArgumentException("Not a valid email address."); } mailMessage.Body = Body; //set the body content to mailmessage mailMessage.BodyFormat = IsHtml ? MailFormat.Html : MailFormat.Text; // check the body type // AlternateView bodyView = AlternateView.CreateAlternateViewFromString(Body, null, "text/Html"); SetToaddress(mailMessage, null); // set to address to mailmessage SetBccAddress(mailMessage, null); //set All bcc address if bcc is added SetCcAddress(mailMessage, null); //set all Cc address if Cc is added if (Attachments.Count > 0) { //set attachments to mailmessage SetAttachments(mailMessage, null); } if (ToFlag == 1 && BccFlag == 1 && CcFlag == 1) { //if all the flag set to 1 then return false and set a status message throw new ArgumentException("No recipients specified Please Specify any recipients"); } //if flag are not set to 1 then it consist one or more recipients then send the mailmessage using smtpMail.send() and return true SmtpMail.Send(mailMessage); DeleteAttachmentFiles(); return(true); } catch (Exception ex) { throw ex; } }
/// <summary> /// An overloaded function send mail without any argument /// </summary> /// <returns>bool value</returns> public bool SendMail() { try { //Check mail is system.net.mail type or system.web.mail type if (IsSmtpClientMail) //if system.net.mail { MailMessage mailMsg = new MailMessage(); //call the over loaded function send mail with one argument of system.net.mail.mailmessage type // mailMsg.AlternateViews.Add(mailMsg); bool response = SendMail(mailMsg); if (response) { UpdateDatabase(); } return(response); } else { //system.web.mail call another overloaded function System.Web.Mail.MailMessage mailMessage = new System.Web.Mail.MailMessage(); //call the over loaded function send mail with one argument of system.web.mail.mailmessage type bool response = SendMail(mailMessage); if (response) { UpdateDatabase(); } return(response); } } catch (Exception ex) { throw ex; } }
public static void sendEmail(string frommail, string password, string tomail, string ccmail, string bccmail, string subject, string body) { try { System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage(); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.exmail.qq.com"); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465"); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendusing", "2"); //sendusing: cdoSendUsingPort, value 2, for sending the message using //the network. //smtpauthenticate: Specifies the mechanism used when authenticating //to an SMTP //service over the network. Possible values are: //- cdoAnonymous, value 0. Do not authenticate. //- cdoBasic, value 1. Use basic clear-text authentication. //When using this option you have to provide the user name and password //through the sendusername and sendpassword fields. //- cdoNTLM, value 2. The current process security context is used to // authenticate with the service. myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //Use 0 for anonymous myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", frommail); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); myMail.From = frommail; //if (!String.IsNullOrWhiteSpace(bccmail)) //{ // string[] maillist = YSWL.Common.StringPlus.GetStrArray(bccmail); // foreach (string m in maillist) // { // if (m.Trim() != "") // { // MailAddress bcc = new MailAddress(m.Trim()); // myMail.Bcc // } // } //} //if (!String.IsNullOrWhiteSpace(ccmail)) //{ // string[] maillist = YSWL.Common.StringPlus.GetStrArray(ccmail); // foreach (string m in maillist) // { // if (m.Trim() != "") // { // MailAddress cc = new MailAddress(m.Trim()); // message.CC.Add(cc); // } // } //} myMail.Bcc = bccmail; myMail.Subject = subject; myMail.BodyFormat = new MailFormat(); myMail.Body = body; //if (pAttachmentPath.Trim() != "") //{ // MailAttachment MyAttachment = // new MailAttachment(pAttachmentPath); // myMail.Attachments.Add(MyAttachment); // myMail.Priority = System.Web.Mail.MailPriority.High; //} System.Web.Mail.SmtpMail.SmtpServer = "smtp.exmail.qq.com:465"; System.Web.Mail.SmtpMail.Send(myMail); } catch (Exception ex) { throw; } }
public static bool SendEmail( string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, System.Web.Mail.MailFormat pFormat, string pAttachmentPath) { try { System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage(); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpserver", SiteSetting.Get <string>("SMTP")); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpserverport", SiteSetting.Get <Int32>("SMTPPort")); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendusing", "2"); //sendusing: cdoSendUsingPort, value 2, for sending the message using //the network. //smtpauthenticate: Specifies the mechanism used when authenticating //to an SMTP //service over the network. Possible values are: //- cdoAnonymous, value 0. Do not authenticate. //- cdoBasic, value 1. Use basic clear-text authentication. //When using this option you have to provide the user name and password //through the sendusername and sendpassword fields. //- cdoNTLM, value 2. The current process security context is used to // authenticate with the service. myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //Use 0 for anonymous myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", pGmailEmail); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", pGmailPassword); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpusessl", SiteSetting.Get <bool>("SMTPSSL") ? "true" : "false"); myMail.From = pGmailEmail; myMail.To = pTo; myMail.Subject = pSubject; myMail.BodyFormat = pFormat; myMail.Body = pBody; if (pAttachmentPath.Trim() != "") { MailAttachment MyAttachment = new MailAttachment(pAttachmentPath); myMail.Attachments.Add(MyAttachment); myMail.Priority = System.Web.Mail.MailPriority.Normal; } System.Web.Mail.SmtpMail.SmtpServer = SiteSetting.Get <string>("SMTP") + ":" + SiteSetting.Get <Int32>("SMTPPort"); System.Web.Mail.SmtpMail.Send(myMail); return(true); } catch (Exception ex) { throw; } }