2013-03-14 62 views
0

我正在將我的sitefinity網站項目轉換爲Web應用程序。當前上下文中不存在「電子郵件」名稱(Web應用程序)

我們有一個最初在網站上工作的用戶控件,但現在我們在構建Web應用程序時出現以下錯誤。

「電子郵件」並不在當前環境中存在的名稱(線104)

using System; 
using System.Collections.Generic; 
using System.Text.RegularExpressions; 
using System.Web.Security; 
using System.Net.Mail; 
using Telerik.Sitefinity.Security; 
using Telerik.Sitefinity.Security.Model; 
using Telerik.Sitefinity.Model; 


namespace UserControls.Saville 
{ 

    public partial class CustomUserRegistration : System.Web.UI.UserControl 
    { 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (Request.Url.Query.Contains("?registered")) 
      { 
       userRegistration.Visible = false; 
       RegSuccess.Text = "We have emailed you an account activation link. Please check your emails."; 
      } 
     } 

     protected void btnRegister_OnClick(object sender, EventArgs e) 
     { 
      Match emailValid = Regex.Match(tbxRegEmail.Text, @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" 
                   + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? 
                  [0-9]{1,2}|25[0-5]|2[0-4][0-9])\." 
                   + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? 
                  [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" 
                   + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$"); 
      if (tbxFirstName.Text != String.Empty && 
       tbxSurname.Text != String.Empty && 
       tbxJobTitle.Text != String.Empty && 
       tbxOrganisation.Text != String.Empty && 
       tbxTelephone.Text != String.Empty && 
       tbxAddressLine1.Text != String.Empty && 
       tbxAddressCity.Text != String.Empty && 
       tbxAddressPostcode.Text != String.Empty && 
       tbxAddressCounty.Text != String.Empty && 
       tbxAddressCountry.Text != String.Empty && 
       tbxRegEmail.Text != String.Empty && 
       tbxRegUsername.Text != String.Empty && 
       tbxRegPassword.Text != String.Empty && 
       tbxCPassword.Text != String.Empty && 
       emailValid.Success && 
       tbxRegPassword.Text.Length > 5 && 
       tbxRegPassword.Text == tbxCPassword.Text) 
      { 
       var manager = UserManager.GetManager("AspNetSqlMembershipProvider"); 
       manager.Provider.SuppressSecurityChecks = true; 

       if (manager.UserExists(tbxRegUsername.Text)) 
       { 
        RegError.Text = "Username already in use."; 
       } 
       else 
       { 
        if (manager.EmailExists(tbxRegEmail.Text)) 
        { 
         RegError.Text = "Email Address is already registered"; 
        } 
        else 
        { 
         MembershipCreateStatus status; 
         User user = manager.CreateUser(tbxRegUsername.Text, tbxRegPassword.Text, tbxRegEmail.Text, "Question", "Answer", false, null, out status); 

         manager.SaveChanges(); 

         if (status == MembershipCreateStatus.Success) 
         { 
          UserProfileManager profileManager = UserProfileManager.GetManager(); 
          profileManager.Provider.SuppressSecurityChecks = true; 

          SitefinityProfile userProfile = profileManager.CreateProfile(user, Guid.NewGuid(), typeof(SitefinityProfile)) as SitefinityProfile; 
          if (userProfile != null) 
          { 
           userProfile.SetValue("Title", ddlTitle.SelectedItem.Text); 
           userProfile.FirstName = tbxFirstName.Text; 
           userProfile.LastName = tbxSurname.Text; 
           userProfile.SetValue("JobTitle", tbxJobTitle.Text); 
           userProfile.SetValue("Organisation", tbxOrganisation.Text); 
           userProfile.SetValue("MainPhone", tbxTelephone.Text); 
           userProfile.SetValue("MobilePhone", tbxMobileTelephone.Text); 
           userProfile.SetValue("Address_Street_1", tbxAddressLine1.Text); 
           userProfile.SetValue("Address_Street_2", tbxAddressLine2.Text); 
           userProfile.SetValue("Address_City", tbxAddressCity.Text); 
           userProfile.SetValue("Address_PostcodeZIP", tbxAddressPostcode.Text); 
           userProfile.SetValue("Address_CountyState", tbxAddressCounty.Text); 
           userProfile.SetValue("Address_Country", tbxAddressCountry.Text); 
           userProfile.SetValue("Registration_Code", tbxRegCode.Text); 
           userProfile.SetValue("SendMarketing", chkbxMaterials.Checked); 
           profileManager.SaveChanges(); 

           RoleManager roleManager = RoleManager.GetManager("AspNetSqlRoleProvider"); 
           roleManager.Provider.SuppressSecurityChecks = true; 
           Role untrained = roleManager.GetRole("Untrained"); 
           roleManager.AddUserToRole(user, untrained); 

           roleManager.SaveChanges(); 

           String emailContent = String.Format("{1}, <br/><br/>Your registration is nearly complete.<br/><br/>Please follow this link to activate your account:<br/><a href='{2}/login?ID={3}'>{2}/login?ID={3}</a>", Environment.NewLine, tbxFirstName.Text, Request.Url.GetLeftPart(UriPartial.Authority), user.Id.ToString()); 
           Email.Send(tbxRegEmail.Text, "[email protected]*****.com", "*****", emailContent); 

           Response.Redirect("~/login?registered"); 
          } 

          RegError.Text = "An error occurred whilst registering. If problem persists please contact [email protected]*****.com"; 
         } 
        } 
       } 
      } 
      else 
      { 
       RegError.Text = "Please check all required fields have been filled and are valid."; 
      } 
     } 
    } 
} 

本來我們有項目中的不同的文件夾內的Email.cs文件,但似乎是Web應用程序無法找到此。

+0

添加一個完全Quilified名稱的電子郵件類像Mynamespace.Email.Send(.....) – JSJ 2013-03-14 11:55:39

+0

不幸的是,我已經嘗試過這一點,但我們只是得到同樣的錯誤,但這次是'MYnamespace'.Email。發送(... – LaurenceP 2013-03-14 12:05:07

+1

只需確認:您是否驗證過電子郵件的名稱空間(在Email.cs中)是否正確,並且該類是公共的? – Kjartan 2013-03-14 12:26:24

回答

1

我可能會推薦一個稍微不同的解決方案來發送電子郵件。使用MailDefinitionMailMessage類創建電子郵件的正文,然後從此處使用類別EmailSender以及Sitefinity中的SMTP設置發送電子郵件。下面的示例代碼:

using System.Net.Mail; 
using Telerik.Sitefinity.Services; 
using Telerik.Sitefinity.Web.Mail; 
using Telerik.Sitefinity.Configuration; 

var smtpSettings = Config.Get<SystemConfig>().SmtpSettings; 
MailDefinition mailDef = new MailDefinition() 
{ 
    IsBodyHtml = true, 
    BodyFileName = "~/Files/EmailBody.html", 
    Subject = "Thanks for Commenting!", 
    From = !smtpSettings.DefaultSenderEmailAddress.IsNullOrEmpty() ? smtpSettings.DefaultSenderEmailAddress : smtpSettings.UserName 
}; 
MailMessage email = mailDef.CreateMailMessage(this.EmailControl.Value.ToString(), new ListDictionary(), this); 
EmailSender.Get().Send(email); 

ListDictionary正在用代碼中的值替換正文中的值。如果你不需要,你可以只包含一個空字典。

+0

嗨,大家好,感謝您的幫助,我明白了。在將網站轉換爲webapp時,我必須編譯email.cs,而不是將它留在「內容」中。有些東西太簡單了,但我無法找到答案的年齡! – LaurenceP 2013-03-15 09:24:25

相關問題