public void UpgradeWebconfig(string from, string host, string username, string pass, int port) { var myConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); System.Net.Configuration.MailSettingsSectionGroup mailSection = myConfig.GetSectionGroup("system.net/mailSettings") as System.Net.Configuration.MailSettingsSectionGroup; if (mailSection != null) { mailSection.Smtp.From = @from; mailSection.Smtp.Network.Host = host; mailSection.Smtp.Network.UserName = username; mailSection.Smtp.Network.Password = pass; mailSection.Smtp.Network.Port = port == 0 ? 1 : port; } myConfig.Save(ConfigurationSaveMode.Modified); }
private bool SendEmail(string To, string From, string CC, string BCC, string Subject, string Body, ArrayList EmailAttechment) { //To = "*****@*****.**"; //========== Find the config application path ==========// System.Configuration.Configuration Config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath); //========== Find the mail setting from the web config file ==========// System.Net.Configuration.MailSettingsSectionGroup MailSettings = (System.Net.Configuration.MailSettingsSectionGroup)Config.GetSectionGroup("system.net/mailSettings"); //========== Obtain the Network Credentials from the mailSettings section ==========// NetworkCredential Credential = new NetworkCredential(MailSettings.Smtp.Network.UserName, MailSettings.Smtp.Network.Password); //========== Create the SMTP Client ==========// System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient(); //========== Assign the host address ==========// Client.Host = MailSettings.Smtp.Network.Host; //========== Assign the network credentials to the smtp client ==========// Client.Credentials = Credential; //========== Create the mail object ==========// System.Net.Mail.MailMessage ObjEmail = new System.Net.Mail.MailMessage(); //========== Assign the values to the mail object ==========// if (From == "") { ObjEmail.From = new System.Net.Mail.MailAddress(From); } else { ObjEmail.From = new System.Net.Mail.MailAddress(From); } string[] _Str_Email_Address = To.Split(';'); foreach (string item in _Str_Email_Address) { ObjEmail.To.Add(item); } if (BCC.Length != 0) { ObjEmail.Bcc.Add(BCC); } if (CC.Length != 0) { ObjEmail.CC.Add(CC); } ObjEmail.Subject = Subject.Replace("\n", " ").Replace("\r", " ").Replace("\t", ""); ObjEmail.IsBodyHtml = true; ObjEmail.Priority = System.Net.Mail.MailPriority.Normal; ObjEmail.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnFailure; ObjEmail.Body = Body; //========== Check the email attachment for the email ==========// if (EmailAttechment != null && EmailAttechment.Count != 0) { for (int i = 0; i <= EmailAttechment.Count - 1; i++) { System.Net.Mail.Attachment attachFile = new System.Net.Mail.Attachment(EmailAttechment[i].ToString()); ObjEmail.Attachments.Add(attachFile); } } try { Client.Send(ObjEmail); } catch (Exception Exc) { Exc.Message.ToString(); return(false); } return(true); }
//end str format /// <summary> /// Sends an mail message /// use this simplified version instead of mailhelper class /// so we can stram attachment without having to save it as a file /// </summary> public static string send_spreadsheet(string[] sendto, string subject, string msg, string named, IList <Attachment> attachments) { // Instantiate a new instance of MailMessage MailMessage mMailMessage = new MailMessage(); string _result = string.Empty; // Set the sender address of the mail message //do we just default from web.config? //mMailMessage.From = new MailAddress(mrcTo.rEmail); for (int _ix = 0; _ix < sendto.Length; _ix++) { mMailMessage.To.Add(new MailAddress(sendto[_ix])); } // Set the recipient address of the mail message if (!String.IsNullOrEmpty(msg)) { if (attachments.Count > 0) { for (int _ix = 0; _ix < attachments.Count; _ix++) { // Stream the Excel spreadsheet to the client in a format // compatible with Excel 97/2000/XP/2003/2007/2010. //System.IO.MemoryStream _stream = new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(attached)); //Attachment _a = new Attachment(_stream, named + ".csv"); mMailMessage.Attachments.Add(attachments[_ix]); } } // Set the subject of the mail message mMailMessage.Subject = subject; // Set the body of the mail message mMailMessage.Body = msg; // Set the format of the mail message body as HTML mMailMessage.IsBodyHtml = true; // Set the priority of the mail message to normal mMailMessage.Priority = MailPriority.Normal; try { SmtpClient mSmtpClient = new SmtpClient(); string ConfigPath = "~\\Web.config"; Configuration configurationFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(ConfigPath); //send the message System.Net.Configuration.MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as System.Net.Configuration.MailSettingsSectionGroup; if (mailSettings != null) { int port = mailSettings.Smtp.Network.Port; string host = mailSettings.Smtp.Network.Host; string password = mailSettings.Smtp.Network.Password; string username = mailSettings.Smtp.Network.UserName; mSmtpClient.Port = Convert.ToInt32(port); if (username != null && username != "") { //to authenticate we set the username and password properites on the SmtpClient mSmtpClient.Credentials = new System.Net.NetworkCredential(username, password); } // Send the mail message //***************** mSmtpClient.Send(mMailMessage); //***************** } } catch (SmtpException ex) { //A problem occurred when sending the email message _result = ex.ToString(); } } return(_result); }