0

我正在将我的 sitefinity 网站项目转换为 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, "support.manager@*****.com", "*****", emailContent);

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

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

最初我们在项目的不同文件夹中有一个 Email.cs 文件,但 webapp 似乎无法找到它。

4

1 回答 1

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正文中的值替换为代码中的值。如果你不需要它,你可以只包含一个空字典。

于 2013-03-14T18:00:55.820 回答