如何使用 asp.net 其他功能的身份邮件功能 [显示错误]

How to use asp.net Identity mailing feature for other functions [Error displayed]

我正在开发一个基于论坛的项目,用户可以在其中提交 posts。该项目是使用 Identity 在 ASP.Net 的网络表单上创建的。

在 IdentityConfig.cs class 中,负责创建电子邮件结构的代码如下

public class EmailService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your email service here to send an email.
            //return Task.FromResult(0);
            return Task.Factory.StartNew(() =>
            {
                sendMail(message);
            });
        }

        void sendMail(IdentityMessage message)
        {
            #region formatter
            string text = string.Format("Please click on this link to {0}: {1}", message.Subject, message.Body);
            string html = "Please confirm your account by clicking this link: <a href=\"" + message.Body + "\">link</a><br/>";

            html += HttpUtility.HtmlEncode(@"Or click on the copy the following link on the browser:" + message.Body);
            #endregion

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"].ToString());
            msg.To.Add(new MailAddress(message.Destination));
            msg.Subject = message.Subject;
            msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
            msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Email"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
            smtpClient.Credentials = credentials;
            smtpClient.EnableSsl = true;
            smtpClient.Send(msg);
        }

用户注册后,用于提交电子邮件的代码如下

string code = manager.GenerateEmailConfirmationToken(user.Id);
                string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
                manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

我尝试重新创建以下代码,以便能够为与项目相关的其他功能发送电子邮件。我首先尝试在其下直接制作第二个"void sendmail"如下

public void sendCoordinatorMail(IdentityMessage message)
        {
            #region formatter
            string text = string.Format("A user of your department has submitted a post.");
            string html = "";

            html += HttpUtility.HtmlEncode(@"" + message.Body);
            #endregion

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"].ToString());
            msg.To.Add(new MailAddress(message.Destination));
            msg.Subject = message.Subject;
            msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
            msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Email"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
            smtpClient.Credentials = credentials;
            smtpClient.EnableSsl = true;
            smtpClient.Send(msg);

需要上面的代码来构造一封电子邮件,每当用户创建并提交 post 时,该电子邮件就会发送给论坛管理员,我在 "Post newPost = new Post()" 代码之后添加了以下代码如下

manager.sendCoordinatorMail(user.Id, "A user of your department has submitted a post.", "");

我似乎在 "sendCoordinatorMail" 上收到一个错误,上面写着

'ApplicationUserManager' does not contain a definition for 'sendCoordinatorMail' and no accessible extension method 'sendCoordinatorMail' accepting a first argument of type 'ApplicationUserManager' could be found.

对于这个很长的问题,我深表歉意,但是我明显做错了什么吗?

我能看到的:

您的经理属于ApplicationUserManager类型,您的电子邮件相关方法在EmailService class中。您正试图通过 ApplicationUserManager 调用 sendCoordinatorMail 方法,但我认为管理器中没有类似的方法。

PS:尝试在管理器中设置一个函数来调用 sendCoordinatorMail