public override System.Web.Mvc.ActionResult Emails(System.Net.Configuration.SmtpSection model) { var callInfo = new T4MVC_System_Web_Mvc_ActionResult(Area, Name, ActionNames.Emails); ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "model", model); EmailsOverride(callInfo, model); return(callInfo); }
private void SendEmail(ContactModel objContactModel) { try { //Retrieve the mail server host setting from the web.config file. var credential = new System.Net.Configuration.SmtpSection().Network; MailMessage newMailMessage = new MailMessage(objContactModel.txtEmail, StringConstants.COMPANY_EMAIL); newMailMessage.Subject = string.Format("Enquiry From {0} - {1}", objContactModel.txtName, objContactModel.txtSubject); newMailMessage.Body = objContactModel.txtMessage; SmtpClient mailClient = new SmtpClient(credential.Host); mailClient.Send(newMailMessage); return; } catch (Exception ex) { //we might want to log the error here return; } }
/////////////////////////////////////////////////////////////////////// public static string send_email( string to, string from, string cc, string subject, string body, MailFormat body_format, MailPriority priority, int[] attachment_bpids, bool return_receipt) { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new MailAddress(from); Email.add_addresses_to_email(msg, to, AddrType.to); if (!string.IsNullOrEmpty(cc.Trim())) { Email.add_addresses_to_email(msg, cc, AddrType.cc); } msg.Subject = subject; if (priority == MailPriority.Normal) { msg.Priority = System.Net.Mail.MailPriority.Normal; } else if (priority == MailPriority.High) { msg.Priority = System.Net.Mail.MailPriority.High; } else { priority = MailPriority.Low; } // This fixes a bug for a couple people, but make it configurable, just in case. if (Util.get_setting("BodyEncodingUTF8", "1") == "1") { msg.BodyEncoding = Encoding.UTF8; } if (return_receipt) { msg.Headers.Add("Disposition-Notification-To", from); } // workaround for a bug I don't understand... if (Util.get_setting("SmtpForceReplaceOfBareLineFeeds", "0") == "1") { body = body.Replace("\n", "\r\n"); } msg.Body = body; msg.IsBodyHtml = body_format == MailFormat.Html; StuffToDelete stuff_to_delete = null; if (attachment_bpids != null && attachment_bpids.Length > 0) { stuff_to_delete = new StuffToDelete(); string upload_folder = btnet.Util.get_upload_folder(); if (string.IsNullOrEmpty(upload_folder)) { upload_folder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(upload_folder); stuff_to_delete.directories_to_delete.Add(upload_folder); } foreach (int attachment_bpid in attachment_bpids) { string dest_path_and_filename = convert_uploaded_blob_to_flat_file(upload_folder, attachment_bpid, stuff_to_delete.files_to_delete); // Add saved file as attachment System.Net.Mail.Attachment mail_attachment = new System.Net.Mail.Attachment( dest_path_and_filename); msg.Attachments.Add(mail_attachment); } } try { // This fixes a bug for some people. Not sure how it happens.... msg.Body = msg.Body.Replace(Convert.ToChar(0), ' ').Trim(); System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); // SSL or not string force_ssl = Util.get_setting("SmtpForceSsl", ""); if (force_ssl == "") { // get the port so that we can guess whether SSL or not System.Net.Configuration.SmtpSection smtpSec = (System.Net.Configuration.SmtpSection) System.Configuration.ConfigurationManager.GetSection("system.net/mailSettings/smtp"); if (smtpSec.Network.Port == 465 || smtpSec.Network.Port == 587) { smtpClient.EnableSsl = true; } else { smtpClient.EnableSsl = false; } } else { if (force_ssl == "1") { smtpClient.EnableSsl = true; } else { smtpClient.EnableSsl = false; } } // Ignore certificate errors if (smtpClient.EnableSsl) { ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }; } smtpClient.Send(msg); if (stuff_to_delete != null) { stuff_to_delete.msg = msg; delete_stuff(stuff_to_delete); } return(""); } catch (Exception e) { Util.write_to_log("There was a problem sending email. Check settings in Web.config."); Util.write_to_log("TO:" + to); Util.write_to_log("FROM:" + from); Util.write_to_log("SUBJECT:" + subject); Util.write_to_log(e.GetBaseException().Message.ToString()); delete_stuff(stuff_to_delete); return(e.GetBaseException().Message); } }
//Send the user password to the user email //If link button was removed from the page this method has empty content. private void SendPasswordToUser(bool usecaptcha) { //if there is a user identity table, with an email address field, //then send the user name and password to the user email if (usecaptcha) { if (!(this.Page.IsValid)) { Exception exc = new Exception(this.recaptcha.ErrorMessage); throw exc; } } // The email address is required by validation string uemail = this.Emailaddress.Text; // lookup the email address in the user identity table and abort if not present IUserIdentityRecord userRecord = SystemUtils.GetUserInfoEmailAddr(uemail); if (userRecord == null) { string msg = GetResourceValue("Msg:InvalidEmail") + "<br />"; Exception exc = new Exception(msg); throw exc; } // send the login info to the user email BaseClasses.Utils.MailSenderInThread email = new BaseClasses.Utils.MailSenderInThread(); email.AddTo(uemail); // use the from email address in the smtp section of the web.config, if available string fromadr = uemail; object obj = System.Web.Configuration.WebConfigurationManager.GetSection("system.net/mailSettings/smtp"); if (obj != null) { System.Net.Configuration.SmtpSection smtpcfg = null; smtpcfg = (System.Net.Configuration.SmtpSection)obj; fromadr = smtpcfg.From; } email.AddFrom(fromadr); email.SetSubject(GetResourceValue("Txt:GetSignin")); // Be sure the URL is processed for substitution and encryption string uarg = ((BaseApplicationPage)this.Page).Encrypt(uemail, false); string cultarg = System.Threading.Thread.CurrentThread.CurrentUICulture.Name; if (!(string.IsNullOrEmpty(cultarg))) { cultarg = System.Web.HttpUtility.UrlEncode(cultarg); cultarg = BaseClasses.Web.UI.BasePage.APPLICATION_CULTURE_UI_URL_PARAM + "=" + cultarg; } string SendEmailContentURL = null; string pgUrl = BaseClasses.Configuration.ApplicationSettings.Current.SendUserInfoEmailUrl; if (pgUrl.StartsWith("/")) { pgUrl = pgUrl.Substring(1); } SendEmailContentURL = pgUrl + "?Email=" + System.Web.HttpUtility.UrlEncode(uarg); if (!(string.IsNullOrEmpty(cultarg))) { SendEmailContentURL += "&" + cultarg; } email.AreImagesEmbedded = true; email.RemoveJS = true; email.SetIsHtmlContent(true); email.SetContentURL(SendEmailContentURL, this); try { email.SendMessage(); } catch (Exception ex) { string msg = GetResourceValue("Msg:SendToFailed") + " " + uemail + "<br />" + ex.Message; Exception exc = new Exception(msg); throw exc; } this.ForgotUserInfoLabel.Visible = true; this.ForgotUserInfoLabel.Text = GetResourceValue("Msg:PwdEmailed") + " " + uemail; this.ForgotUserErrorLabel.Text = ""; this.ForgotUserErrorLabel.Visible = false; this.EnterEmailLabel.Visible = false; this.Emailaddress.Visible = false; this.FillRecaptchaLabel.Visible = false; this.recaptcha.SkipRecaptcha = true; this.recaptcha.Visible = false; this.SendButton.Visible = false; }
public EmailTraceListenersTests() { smtpConfig = System.Configuration.ConfigurationManager.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection; pickupDirectory = (smtpConfig != null) ? smtpConfig.SpecifiedPickupDirectory.PickupDirectoryLocation : null; mockSmtpPort = smtpConfig.Network.Port; }
partial void EmailsOverride(T4MVC_System_Web_Mvc_ActionResult callInfo, System.Net.Configuration.SmtpSection model);
/// <devdoc> /// Creates a MailMessage using the body parameter. /// </devdoc> public MailMessage CreateMailMessage(string recipients, IDictionary replacements, string body, Control owner) { if (owner == null) { throw new ArgumentNullException("owner"); } string from = From; if (String.IsNullOrEmpty(from)) { System.Net.Configuration.SmtpSection smtpSection = RuntimeConfig.GetConfig().Smtp; if (smtpSection == null || smtpSection.Network == null || String.IsNullOrEmpty(smtpSection.From)) { throw new HttpException(SR.GetString(SR.MailDefinition_NoFromAddressSpecified)); } else { from = smtpSection.From; } } MailMessage message = null; try { message = new MailMessage(from, recipients); if (!String.IsNullOrEmpty(CC)) { message.CC.Add(CC); } if (!String.IsNullOrEmpty(Subject)) { message.Subject = Subject; } message.Priority = Priority; if (replacements != null && !String.IsNullOrEmpty(body)) { foreach (object key in replacements.Keys) { string fromString = key as string; string toString = replacements[key] as string; if ((fromString == null) || (toString == null)) { throw new ArgumentException(SR.GetString(SR.MailDefinition_InvalidReplacements)); } // DevDiv 151177 // According to http://msdn2.microsoft.com/en-us/library/ewy2t5e0.aspx, some special // constructs (starting with "$") are recognized in the replacement patterns. References of // these constructs will be replaced with predefined strings in the final output. To use the // character "$" as is in the replacement patterns, we need to replace all references of single "$" // with "$$", because "$$" in replacement patterns are replaced with a single "$" in the // final output. toString = toString.Replace("$", "$$"); body = Regex.Replace(body, fromString, toString, RegexOptions.IgnoreCase); } } // If there are any embedded objects, we need to construct an alternate view with text/html // And add all of the embedded objects as linked resouces if (EmbeddedObjects.Count > 0) { string viewContentType = (IsBodyHtml ? MediaTypeNames.Text.Html : MediaTypeNames.Text.Plain); AlternateView view = AlternateView.CreateAlternateViewFromString(body, null, viewContentType); foreach (EmbeddedMailObject part in EmbeddedObjects) { string path = part.Path; if (String.IsNullOrEmpty(path)) { throw ExceptionUtil.PropertyNullOrEmpty("EmbeddedMailObject.Path"); } if (!UrlPath.IsAbsolutePhysicalPath(path)) { VirtualPath virtualPath = VirtualPath.Combine(owner.TemplateControlVirtualDirectory, VirtualPath.Create(path)); path = virtualPath.AppRelativeVirtualPathString; } // The FileStream will be closed by MailMessage.Dispose() LinkedResource lr = null; try { Stream stream = null; try { stream = owner.OpenFile(path); lr = new LinkedResource(stream); } catch { if (stream != null) { ((IDisposable)stream).Dispose(); } throw; } lr.ContentId = part.Name; lr.ContentType.Name = UrlPath.GetFileName(path); view.LinkedResources.Add(lr); } catch { if (lr != null) { lr.Dispose(); } throw; } } message.AlternateViews.Add(view); } else if (!String.IsNullOrEmpty(body)) { message.Body = body; } message.IsBodyHtml = IsBodyHtml; return(message); } catch { if (message != null) { message.Dispose(); } throw; } }